Consider the following scenario.
List list = new ArrayList<>();
Now I added the String values for this li
for-each is syntactic sugar for using iterators (approach 2).
You might need to use iterators if you need to modify collection in your loop. First approach will throw exception.
for (String i : list) {
System.out.println(i);
list.remove(i); // throws exception
}
Iterator it=list.iterator();
while (it.hasNext()){
System.out.println(it.next());
it.remove(); // valid here
}