Search code examples
javaiteratornested-loops

Iteration loop inside another loop


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())
 {
  ...
 }

}

Solution

  • Iterator iter;
    for (int i=0;i<30;i++)
    {
        iter = list.iterator();
        while (iter.hasNext())
         {
          ...
         }
    
    }