iterator
来源:互联网 发布:淘宝折800报名入口 编辑:程序博客网 时间:2024/06/09 20:08
If you are just wandering over the collection to read all of the values, then there is no difference between using an iterator or the new for loop syntax, as the new syntax just uses the iterator underwater.
If however, you mean by loop the old “c-style” loop:
for(int i=0; i< list.size(); i++) {
Object o = list.get(i);
}
Then the new for loop, or iterator, can be a lot more efficient, depending on the underlying data structure. The reason for this is that for some data structures, get(i) is an O(n) operation, which makes the loop an O(n2) operation. A traditional linked list is an example of such a data structure. All iterators have as a fundamental requirement that next() should be an O(1) operation, making the loop O(n).
To verify that the iterator is used underwater by the new for loop syntax, compare the generated bytecodes from the following two Java snippets. First the for loop:
List a = new ArrayList();
for (Integer integer : a)
{
integer.toString();
}
// Byte code
ALOAD 1
INVOKEINTERFACE java/util/List.iterator()Ljava/util/Iterator;
ASTORE 3
GOTO L2
L3
ALOAD 3
INVOKEINTERFACE java/util/Iterator.next()Ljava/lang/Object;
CHECKCAST java/lang/Integer
ASTORE 2
ALOAD 2
INVOKEVIRTUAL java/lang/Integer.toString()Ljava/lang/String;
POP
L2
ALOAD 3
INVOKEINTERFACE java/util/Iterator.hasNext()Z
IFNE L3
And second, the iterator:
List a = new ArrayList();
for (Iterator iterator = a.iterator(); iterator.hasNext();)
{
Integer integer = (Integer) iterator.next();
integer.toString();
}
// Bytecode:
ALOAD 1
INVOKEINTERFACE java/util/List.iterator()Ljava/util/Iterator;
ASTORE 2
GOTO L7
L8
ALOAD 2
INVOKEINTERFACE java/util/Iterator.next()Ljava/lang/Object;
CHECKCAST java/lang/Integer
ASTORE 3
ALOAD 3
INVOKEVIRTUAL java/lang/Integer.toString()Ljava/lang/String;
POP
L7
ALOAD 2
INVOKEINTERFACE java/util/Iterator.hasNext()Z
IFNE L8
As you can see, the generated byte code is effectively identical, so there is no performance penalty to using either form. Therefore, you should choose the form of loop that is most aesthetically appealing to you, for most people that will be the for-each loop, as that has less boilerplate code.
for-each is syntactic sugar for using iterators (approach 2).
You might need to use iterators if you need to modify collection in your loop. First approach will throw exception.
for (String i : list) {
System.out.println(i);
list.remove(i); // throws exception modCount 和 expectedModCount不一致
}
Iterator it=list.iterator();
while (it.hasNext()){
System.out.println(it.next());
it.remove(); // valid here
}
//打印并删除
for (int i = 0; i < a.size(); i++) {
System.out.println(a.get(i));
a.remove(i);
} //不会报错,但影响结果,可以从后往前删
- Iterator
- Iterator
- Iterator
- iterator
- Iterator
- Iterator
- iterator
- iterator
- Iterator
- iterator
- iterator
- iterator
- Iterator
- iterator
- Iterator
- iterator
- iterator
- Iterator()
- centos下完全卸载 mysql
- 第一次使用Android Studio时你应该知道的一切配置(二):新建一个属于自己的工程并安装Genymotion模拟器
- XML与XML解析
- python开发电影查询系统(一)
- 十、网易2017<交错01串>
- iterator
- 正则表达式
- uvalive3523非主流的代码!!求点双连通分量
- 可复用面向对象的软件设计
- 编辑器快捷键的使用
- 隐写工具的相关库使用
- ORA-00001: unique constraint pk_****_key violated oracle imp 语句导入数据对oracle的数据字段操作顺序
- WebCollector 爬百度 搜索关键字排名 为何过几天返回结果全部是0? 10C
- Java设计模式——外观模式