使用迭代器时无限循环 [英] Infinite loop while using iterator

查看:107
本文介绍了使用迭代器时无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Set key1 = map.keySet();
Iterator it1 = key1.iterator();
int cnt=0;
while (it1.hasNext()) {
  cnt++;
}

此代码导致无限循环的可能性有多大?

What are the chances that this code will result in infinite loop?

实际上它会导致无限循环。我怀疑是因为我没有接受 it1.next(); ,是真的吗?

Actually it is resulting in infinite loop. My doubt is it is because I am not taking it1.next(); , is it true?

推荐答案

是的。在你不打电话给 it1.next()之前,它永远不会转到下一个项目。暂停 it1.next()将返回您在列表/集中添加的对象。

Yes. Until you don't call it1.next() it will never move on to next item. Beause it1.next() will return the object which you have added in the list/set.

这篇关于使用迭代器时无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆