在 java 中多次在同一个集合上使用迭代器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5848895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using the iterator over the same set more than once in java
提问by ddriver1
Say I'm iterating over a set in java, e.g.
假设我在 java 中迭代一个集合,例如
Iterator<_someObject_> it = _someObject_.iterator();
well, first I want to go through every object in the set and make a change to each object I visit. So I use:
好吧,首先我想遍历集合中的每个对象并对我访问的每个对象进行更改。所以我使用:
while(it.hasNext()) {
_someObject_ = it.next();
_someObject_.add3(); //made up method
}
Now that I've done this, I decide I want to iterate through the entire set from start to finish once more to carry out another method on them, given that I've just made changes to each element through my first iteration.
既然我已经这样做了,我决定要从头到尾遍历整个集合,以对它们执行另一种方法,因为我刚刚在第一次迭代中对每个元素进行了更改。
Can I just use
我可以用吗
while(it.hasNext())
again?
再次?
I thought maybe the iterator has a pointer so once it's reached the last element on the first run through, if I call while(it.hasNext())
again, the pointer would still be at the end of the set, meaning my second while loop would be pointless.
我想也许迭代器有一个指针,所以一旦它在第一次运行时到达最后一个元素,如果我while(it.hasNext())
再次调用,指针仍将位于集合的末尾,这意味着我的第二个 while 循环将毫无意义。
回答by ditkin
No, you can not do this. Just ask the Iterable object for another iterator.
不,你不能这样做。只需向 Iterable 对象询问另一个迭代器。
回答by Gressie
Sorry, but the contract of an Iterator
states that you can only go through it once.
对不起,但是合同Iterator
规定你只能通过一次。
Given that you can't know for sure the order of the next Iterator, or even the content for that matter (it might change!), you're better off doing all the modifications or method calls in the same block.
鉴于您无法确定下一个 Iterator 的顺序,甚至无法确定相关内容(它可能会更改!),您最好在同一块中进行所有修改或方法调用。
回答by shyam
Iterators are single use only.
迭代器仅供一次性使用。
回答by corlettk
回答by Wim
it.hasNext()
is false after the loop, this is exactly why the while loop ends. If you do another while(it.hasNext())
afterwards it.hasNext()
will still be false so this loop will exit immediately. You need a new iterator for your new loop.
it.hasNext()
循环后为假,这正是 while 循环结束的原因。如果你while(it.hasNext())
之后再做一次it.hasNext()
仍然是假的,所以这个循环将立即退出。您的新循环需要一个新的迭代器。
回答by cabreracanal
It's true that iterators are single use only, but there is a class called ListIterator
that allows you to do this by looking back on the list. An example would be:
确实,迭代器只能一次性使用,但是有一个叫做的类ListIterator
,它允许您通过回顾列表来做到这一点。一个例子是:
ArrayList<String> list = new ArrayList<String>();
list.add("hey!");
list.add("how");
list.add("are");
list.add("you?");
ListIterator it = list.listIterator();
while(it.hasNext()){
System.out.println(it.next() +" ");//you print all the elements
}
//Now you go back. You can encapsulate this while sentence into a void reset method.
while(it.hasPrevious()){
it.previous();
}
//Now you check that you've reached the beginning of the list
while(it.hasNext()){
System.out.println("back " + it.next() +" ");//you print all the elements
}
I think it could be useful for you that class.
我认为这门课可能对你有用。