Enhanced For loop
November 16, 2010 1 Comment
o The enhanced for loop eliminates the need to use java.util.Iterator to iterate over a collection.
List<String> list = new ArrayList<String>();
list.add(“One”); list.add(“Two”); list.add(“Three”);
for (String item : list)
{
System.out.println(item);
}
o The enhanced for loop works with any array or collection class that implements the new java.lang.Iterable interface.
o The Iterable interface defines a single method called iterator() that returns a java.util.Iterator object.
o Unlike using Iterator, the enhanced for loop doesn’t allow you to remove items from the list during iteration.