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);
} //不会报错,但影响结果,可以从后往前删

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 一个人不停的给你发视频聊天怎么办 被谋杀后的身后事应该怎么办 社保交了五年被单位辞退怎么办 桅子花叶子变黄叶杆蔫怎么办 薄荷养的都黄了干掉了怎么办 被烟草局没收的烟要是假烟怎么办 干了10年工程不想干了怎么办 在服务行业当服务员干不下去怎么办 高中生晚上偷着跑出去玩怎么办 货车高速忘记过安全检查站了怎么办 u盘上的文件名称乱码了怎么办 暖气管掉进去一点水泥渣怎么办 暗埋在瓷砖下的暖气管漏水怎么办 埋在瓷砖下面的水管漏水怎么办 埋在瓷砖下的水管漏水怎么办 水压太大把水管撑坏了怎么办 无效安装包与系统不兼容怎么办 圣空法师持五戒范戒了怎么办 美航空要是不改中国台湾标志怎么办 淘宝买的东西质量有问题怎么办 天猫618长达20天c店怎么办 新开的淘宝店铺没有生意怎么办 淘宝账号登陆限制用不了花呗怎么办 闲鱼买家签收后说是空盒怎么办 在咸鱼卖东西买家恶意退货怎么办 淘宝联系不上买家物流返回怎么办 换了支付宝绑定手机号退款怎么办啊 淘宝评价错了追评评价错了怎么办 淘宝给客户退款后还给差评怎么办 淘宝账号处于下单保护状态怎么办 淘宝卖家物流单号写错了怎么办 有个人给我发直播消息怎么办 网贷申请多了现在秒拒怎么办 顺丰快递寄的瓜果坏了怎么办 淘宝退货快递公司填错了怎么办 不小心把淘宝账号注销了怎么办 腾讯视频会员开通一个月贵怎么办 微交易买美国指数输了四千块怎么办 淘宝地址中包含了违禁词怎么办 微信支付失败但是钱扣了怎么办 支付宝向别人收款交易关闭了怎么办