问题
Is there any way I can use an Iterator inside another loop? I am asking this because I can't find a way to bring the iterator at the start of the list again after the first loop is done and therefore at a second loop the iter.hasNext() will give me false
Iterator iter = list.iterator();
for (int i=0;i<30;i++)
{
while (iter.hasNext())
{
...
}
}
回答1:
Iterator iter;
for (int i=0;i<30;i++)
{
iter = list.iterator();
while (iter.hasNext())
{
...
}
}
回答2:
You could just reset the iterator in the outer loop:
for (int i=0;i<30;i++) {
Iterator iter = list.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
}
}
回答3:
The iterator was consumed during first cycle of a loop (i==0). To fix the problem u need to reinitialise the item
reference inside a loop. Best!
来源:https://stackoverflow.com/questions/29956245/iteration-loop-inside-another-loop