The nuances of Java for-each loop
Java.net has posted an article detailing some of the “nuances of Java’s @for-each@ loop”:http://today.java.net/pub/a/today/2006/11/07/nuances-of-java-5-for-each-loop.html. This loop is a new addition to Java 5 and, in my opinion, one of the more welcomed and helpful additions. At least I like it a lot, it definitely saves you typing a fair bit of code.
bq. This article is an in-depth examination of one of the simplest but most pleasing features in Java 5.0–the @for-each@ loop. I present eleven short items discussing various nuances of usage, pitfalls to be aware of, and possible optimizations surrounding the use of the @for-each@ loop. In the first section, I discuss what kind of iterations are possible with the @for-each@. The next section illustrates common programming errors in using the @for-each@ loop. The final section shows how to write new classes that can be used as targets of a @for-each@ loop. I also talk about advanced implementations that allow multiple iterable views; lazily construct objects just in time for iteration; and enable possible generic algorithm and compiler optimizations of the @for-each@ loop.