用Iterator实现遍历集合
2014-05-09 08:43
351 查看
使用Collection类的Iterator,可以方便的遍历Vector, ArrayList, LinkedList等集合元素,避免通过get()方法遍历时,针对每一种对象单独进行编码。
示例:
[java] view
plaincopy
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解释
boolean java.util.Iterator.hasNext()
Returns
return an element rather than throwing an exception.)
Returns:
---------------------------------------------------------
2.next()函数的API解释
Object java.util.Iterator.next()
Returns the next element in the iteration.
Returns:the next element in the iterationThrows:
more elements
[java] view
plaincopy
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
示例:
[java] view
plaincopy
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解释
boolean java.util.Iterator.hasNext()
hasNext
boolean hasNext()
Returns
trueif the iteration has more elements. (In other words, returns
trueif
next()would
return an element rather than throwing an exception.)
Returns:
trueif the iteration has more elements
---------------------------------------------------------
2.next()函数的API解释
Object java.util.Iterator.next()
next
E next()
Returns the next element in the iteration.
Returns:the next element in the iterationThrows:
NoSuchElementException- if the iteration has no
more elements
[java] view
plaincopy
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模式实现遍历集合
- 用Iterator实现遍历集合
- 用Iterator实现遍历集合
- 用Iterator实现遍历集合
- 用Iterator实现遍历集合
- 用Iterator实现遍历集合
- 用Iterator实现遍历集合
- java中Set集合的遍历及实现类比较分析
- Java集合之ArrayList和LinkedList的实现原理以及Iterator详解
- 如何在java中实现List集合的遍历
- JavaSE中Collection集合框架学习笔记(3)——遍历对象的Iterator和收集对象后的排序
- 黑马程序员------集合的遍历Iterator/ListIterator
- struts2的s:iterator 可以遍历 数据栈里面的任何数组,集合等等 以下几个简单的demo:
- 使用迭代器Iterator和增强for循环如何遍历集合
- Iterator iterator():迭代器,集合的专用遍历方式
- s:iterator遍历对象集合
- Java中Set集合 Iterator迭代遍历数组
- Java中遍历集合的三种方法(for循环、for循环jdk1.5的新方法、Iterator接口)
- struts2标签之iterator遍历集合
- Properties类实现键值对的写入和写出(键值对存储)及Properties集合的遍历