Old way to iterate over a collection
List<String> list = new ArrayList<String>();
Iterator<String> iter = list.iterator();
while (iter.hasNext())
{
String s = iter.next();
System.out.println(s);
}
Iterator<String> iter = list.iterator();
while (iter.hasNext())
{
String s = iter.next();
System.out.println(s);
}
New way using a for loop
List<String> list = new ArrayList<String>();
for (String s : list)
{
System.out.println(s);
}
for (String s : list)
{
System.out.println(s);
}
Note that the for-loop is just syntactic sugar for iterating a collection.
If the collection is modified during the iteration and you are not using a thread-safe collection such as CopyOnWriteArrayList then a ConcurrentModificationException can be thrown.