Consider:
List<String> someList = new ArrayList<String>();
// add "monkey", "donkey", "skeleton key" to someList
for (String item : someList) {
System.out.println(item);
}
What would the equivalent for
loop look like without using the for each syntax?
It would look something like this. Very crufty.
There is a good writeup on for each in the Sun documentation.
As defined in JLS for-each loop can have two forms:
If the type of Expression is a subtype of
Iterable
then translation is as:If the Expression necessarily has an array type
T[]
then:Java 8 has introduced streams which perform generally better. We can use them as:
It's implied by nsayer's answer, but it's worth noting that the OP's for(..) syntax will work when "someList" is anything that implements java.lang.Iterable -- it doesn't have to be a list, or some collection from java.util. Even your own types, therefore, can be used with this syntax.
Iterates over all the objects in Items table.
This looks crazy but hey it works
This works. Magic
In Java 8 features you can use this:
Output