对集合类Vector和Enumeration的应用
来源:互联网 发布:游戏数据修改器 编辑:程序博客网 时间:2024/06/03 16:48
代码如下:
import java.util.*;public class TestVector { public static void main(String[] args) { int b = 0; Vector v = new Vector(); System.out.println("please enter number:"); while (true) { try { b = System.in.read(); } catch (Exception e) { e.printStackTrace(); } if (b == '\r' || b == '\n') break; else { int num = b - '0'; v.addElement(new Integer(num)); } } int sum = 0; Enumeration e = v.elements(); while (e.hasMoreElements()) { Integer intObj = (Integer) e.nextElement(); // Enumeration接受的都为object类型的数据,所以要强制转换 sum += intObj.intValue(); } System.out.println(sum); }}
运行结果:
要仔细体会Vector和Enumeration的应用。
说道Vector和Enumeration就想到Collection和Iterator,用Collection和Iterator实现上面例子的代码如下:
import java.util.*;public class TestCollection { public static void main(String[] args) { int b = 0; ArrayList v = new ArrayList(); System.out.println("please enter number:"); while (true) { try { b = System.in.read(); } catch (Exception e) { e.printStackTrace(); } if (b == '\r' || b == '\n') break; else { int num = b - '0'; v.add(new Integer(num)); } } int sum = 0; Iterator e = v.iterator(); while (e.hasNext()) { Integer intObj = (Integer) e.next(); sum += intObj.intValue(); } System.out.println(sum); }}
运行结果:
Vector和Enumeration内部实现了多线程同步,所以在使用多线程时就应该用Vector和Enumeration实现,而Collection和Iterator没有实现多线程同步,所以运行速度较快,但在多线程的应用程序就要自己编写线程的同步。
1 0
- 对集合类Vector和Enumeration的应用
- 集合类 Vector && Enumeration
- 集合类----Vector与Enumeration接口
- Enumeration遍历Vector集合
- lesson6.5集合类vector和Enumeration、Collection和Iterator、Collections
- 集合类Vector的应用
- Vector和Enumeration
- Enumeration和Iterator遍历集合类
- 使用Enumeration和Iterator遍历集合类
- Java_List_ArrayList,Vector,LinkedList(含Enumeration枚举应用)
- Vector,和vector的应用
- Vector & Enumeration
- 4.2.1 集合类Vetor类和Enumeration接口
- 使用Enumeration和Iterator遍历集合类详解
- Java中vector的使用详解、enumeration枚举接口的和iterator的区别、vector与list的对比
- java集合18--Iterator和Enumeration比较
- java集合18--Iterator和Enumeration比较
- java集合--Iterator和Enumeration比较
- iOS在更改用户头像并保存至本地沙盒目录中对于UIImagePickerController、UIAlertController的使用
- mongodb查看数据库和表的信息
- VSCode配置C++编写环境
- 递归3_计算多个数字
- MySQL数据类型之CHAR与VARCHAR及row size max=65535bytes
- 对集合类Vector和Enumeration的应用
- VLC设置串流的TTL值
- ios CAShapeLayer和UIBezierPath
- 设计模式
- UML系列图-用例图
- 设计模式 单例模式 优雅的单例模式
- CUDA编程(四)并行化我们的程序
- unity3d的playmaker插件使用教程,四、物品破坏,道具拾取
- p354拓扑排序