java 如何在java中重置迭代器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36813035/
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
How to reset iterator in java?
提问by Hassan Farooqi
How do I reset reset an iterator to its first element?
如何重置将迭代器重置为其第一个元素?
Below, I have an Sigma
iterator. How do I reset the iterator to its first element when the while
loop completes?
下面,我有一个Sigma
迭代器。while
循环完成后如何将迭代器重置为其第一个元素?
Iterator it = Sigma.iterator();
while(it.hasNext()){
letter= (String) it.next();
System.out.println(letter);
}
回答by Boann
You do not need to reset it, and cannot do so. Collections are not like a cassette tape that you need to rewind. You can simply discard the old iterator.
您不需要重置它,也不能这样做。收藏品不像需要倒带的盒式磁带。您可以简单地丢弃旧的迭代器。
Every time you call Sigma.iterator()
again it will return a fresh iterator which starts iterating at the beginning of the collection.
每次您Sigma.iterator()
再次调用时,它都会返回一个新的迭代器,该迭代器在集合的开头开始迭代。
回答by Raymond Kelly
I haven't done much work with Iterators but I'm guessing you could set the iterator to its original state.
我没有对迭代器做过多少工作,但我猜你可以将迭代器设置为其原始状态。
Iterator it = Sigma.iterator();
while(it.hasNext()){
letter= (String) it.next();
System.out.println(letter);
}
it = Sigma.iterator();