Java 5: Foreach loops

This will be included on the 2007 AP exam...

A simpler way of iterating over a collection. Anywhere you could use an Iterator before, you can instead write:

for(String name : students) {
    System.out.println(name);
}
This largely removes the need for using Iterator objects explicitly. The Iterator are still needed, though, for those times that you want to use the remove method. For this reason, they are still in the AP course.

You can do a similar thing with arrays.

String[] students = { "John", "Paul", "George", "Ringo" };
for(String name : students) {
    System.out.println(name);
}

Generally speaking, students will understand the foreach loop after 10 minutes of instruction. Personally, though, I mention it later in the course so that they are forced to become familiar with Iterator.

(next)