用Iterator实现遍历集合
使用Collection类的Iterator,可以方便的遍历Vector, ArrayList, LinkedList等集合元素,避免通过get()方法遍历时,针对每一种对象单独进行编码。
示例:
- Collection coll = new Vector(); //LinkedList(); //ArrayList();
- coll.add("Tody");
- coll.add("is");
- coll.add("Sunday.");
- // Output all elements by iterator
- Iterator it = coll.iterator();
- while(it.hasNext()) {
- System.out.print(it.next() + " ");
- }
输出:
Tody is Sunday.
1.hasNext()函数的API解释
hasNext
boolean hasNext()
- Returns
trueif the iteration has more elements. (In other words, returnstrueifnext()would return an element rather than throwing an exception.)
- Returns:
trueif the iteration has more elements
---------------------------------------------------------
2.next()函数的API解释
next
E next()
- Returns the next element in the iteration.
-
- Returns:
- the next element in the iteration
- Throws:
NoSuchElementException- if the iteration has no more elements
- Collection coll = new HashSet();
- coll.add("Tody");
- coll.add("is");
- coll.add("Sunday.");
- // Output all elements by iterator
- Iterator it = coll.iterator();
- while(it.hasNext()) {
- System.out.print(it.next() + " ");
- }
输出:
is Sunday. Tody
由上面两个例子看出,在List和Set对象中,Iterator的next()方法返回的值是不一样的。
原因是List属于线性集合,元素是有序的,读取时是按照数组的形式,一个接一个的读取,存储也是按照add的顺序添加的。
而Set属于非线性的,是无序的,所以读取的元素与添加的顺序不一定一致。
对于HashSet,其实它返回的顺序是按Hashcode的顺序。
如果迭代也有序,则可以用LinkedHashSet。
http://topic.csdn.net/u/20101227/09/63a23d05-7f15-4b0e-9287-e97f96ba4349.html?77188351
用Iterator实现遍历集合的更多相关文章
- 增强for循环遍历集合或数组
遍历:for循环遍历数组或集合:iterator迭代器遍历集合:还有增强for循环(for each)遍历数组或集合: 遍历数组: 遍历集合:
- 遍历集合的Iterator删除其中的元素
package list; import java.util.LinkedList; /* * 遍历集合的时候删除其中的元素 从后往前删,每次都删除的是最后一个元素,不涉及移位 */public cl ...
- java8 增强的Iterator遍历集合元素
Iterator接口也是Java集合框架的成员,与Collection和Map两个系列的集合不一样的是Collection和Map系列主要用于充当容器的作用,而Iterator正如其名字一样是主要用于 ...
- 如何边遍历集合边删除元素--使用Iterator中的remove()方法
在遍历集合时,想将符合条件的某些元素删除,开始是用了下面的方法 public static void main(String[] args) throws UnsupportedEncodingExc ...
- 迭代器:遍历集合元素的操作. iterator()
package seday11; import java.util.ArrayList;import java.util.Collection;import java.util.Iterator; / ...
- 8.2.2 使用Java8增强的Iterator遍历集合元素
8.2.2 使用Java 8增强的Iterator遍历集合元素 Iterator接口方法 程序示例 Iterator仅用于遍历集合 Iterator必须依附于Collection对象 修改迭代变量的值 ...
- 集合—collection、iterator遍历集合
一.collection接口 1.collection常用方法 点击查看代码 @Test public void test(){ //contains() Collection coll = new ...
- Lambda表达式遍历集合
1.Collection Java 8 为Iterable接口新增了一个forEach(Consumer action)默认方法,该方法所需参数的类型是一个函数式接口,而Iterable接口是Coll ...
- JAVA基础学习之final关键字、遍历集合、日期类对象的使用、Math类对象的使用、Runtime类对象的使用、时间对象Date(两个日期相减)(5)
1.final关键字和.net中的const关键字一样,是常量的修饰符,但是final还可以修饰类.方法.写法规范:常量所有字母都大写,多个单词中间用 "_"连接. 2.遍历集合A ...
随机推荐
- Hive shell 命令
Hive shell 命令. 连接 hive shell 直接输入 hive 1.显示表 hive> show tables; OK test Time taken: 0.17 seconds, ...
- epel源报错怎么解决?
http://mirrors.aliyun.com/centos/6/extras/x86_64/repodata/81fdefdd048c01dcc6cda1fd53aacec2a0613ea10d ...
- Cygwin镜像使用帮助
Cygwin镜像使用帮助 收录架构 x86 x86_64 收录版本 所有版本 更新时间 每12小时更新一次 使用说明 选择从互联网安装, 在"User URL"处输入以下地址 ...
- C语言 字面量
在计算机科学中,字面量(literal)是用于表达源代码中一个固定值的表示法(notation). 几乎所有计算机编程语言都具有对基本值的字面量表示,诸如:整数.浮点数以及字符串: 而有很多也对布尔类 ...
- delete指针以后应赋值为NULL——QT deletelater指针以后也同样要马上赋值为NULL
delete p后,只是释放了指针指向的内存空间.p并不会自动被置为NULL,而且指针还在,同时还指向了之前的地址 delete NULL编译器不会报错(因为delete空指针是合法的) 例: 对一个 ...
- 关于Cocos2d-x随机数的生成
1.使用前必须下一个随机种子,可以让每一次生成的随机数是不一样的,这里的每一次指的是时间上的每一次,如果是同一时间的随机数就不能这样写了 srand((unsigned)time(NULL));--- ...
- php -- realpath($path) 函数
PHP realpath路径函数会检测$path指向的目标文件(或文件夹)是否真实存在,相当于调用了file_exists($path). 1.如果目标文件存在且不是符号连接(linux下俗称“软链接 ...
- vnc远程控制软件怎么用
CC是一款不错的局域网控制软件,它的轻便让人无法相信,下载过该软件的人都知道,该软件只有大小 工具/原料 我这里使用的是vnc-E4_2_9X32中文版 被控制端的安装 1 我们先来被控制电脑 ...
- 监视在input框中按下回车(enter) js实现
function getKey(event) { if (event.keyCode == 13) { alert("我是回车键"); } } <input type=&qu ...
- js中的var
vars变量预解析 JavaScript中,你可以在函数的任何位置声明多个var语句,并且它们就好像是在函数顶部声明一样发挥作用,这种行为称为 hoisting(悬置/置顶解析/预解析).当你使用了一 ...