Return to Snippet

Revision: 2495
at March 1, 2007 10:47 by cpjobling


Updated Code
List<String> listOfStrings = new LinkedList<String>( );

listOfStrings.add("Why");
listOfStrings.add("Iterate");
listOfStrings.add("When");
listOfStrings.add("You");
listOfStrings.add("Can");
listOfStrings.add("Avoid");
listOfStrings.add("It!");

for (String s: listOfStrings) {
    System.out.println(s);
}

Revision: 2494
at March 1, 2007 10:44 by cpjobling


Initial Code
List<String> listOfStrings = new LinkedList<String>( );

listOfStrings.add("Why");
listOfStrings.add("Iterate");
listOfStrings.add("When");
listOfStrings.add("You");
listOfStrings.add("Can");
listOfStrings.add("Avoid");
listOfStrings.add("It!");

for (String s: listOfStrings) {
    out.println(s);
}

Initial URL

                                

Initial Description
This code uses the new Java 5 foreach loop to iterate through every element of a list. It avoids the need to declare an iterator.

Initial Title
Iteration through a Generic List (Java 5 version)

Initial Tags
list

Initial Language
Java