我需要为for循环的每一次迭代迭代一组值,但只有在第一次迭代时它才能正常工作。此后,itr.hasNext()
返回false
。
Iterator<String> itr = getQuestionIterator(File file);
for(Person p : persons)
{
while(itr.hasNext())
{
String question = itr.next();
........
........
}
}
这种行为对我来说很清楚。
一种解决方案可能是在for循环中调用getQuestionIterator(File file)
方法,以便每次for循环迭代时都会重新初始化。但这是一种非常低效的方法,因为itr
是独立的。
我试过这个Iterator<String> temp = itr
,但它也不起作用,因为它只包含引用。
有没有办法将迭代器复制到另一个迭代器或其他更好的方法?
https://stackoverflow.com/questions/29961456
复制相似问题