[英]why iterator.next() returns the same item every time?
I have this code: 我有这个代码:
for (int j = 0; j < 7; j++) {
if (failureCountAndDUrls.urls.iterator().hasNext()) {
P p2 = new P().appendText("First "+min+" of "+failureCountAndDUrls.count+":");
String id = failureCountAndDUrls.urls.iterator().next();
}
}
urls
is a Set<String>
urls
是一个Set<String>
and yet .next();
然而
.next();
returns the same item over and over. 一遍又一遍地返回相同的项目。 Even though there are 7 items
即使有7个项目
how can iterate all the items correctly? 如何正确迭代所有项目?
The problem is you're creating a new iterator every time, and each new iterator has the same initial state. 问题是你每次都在创建一个新的迭代器,每个新的迭代器都有相同的初始状态。
Reuse the iterator instead. 改为重用迭代器。
Iterator<String> it = failureCountAndDUrls.urls.iterator();
for (int j = 0; j < 7; j++) {
if (it.hasNext()) {
P p2 = new P().appendText("First "+min+" of "+failureCountAndDUrls.count+":");
String id = it.next();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.