Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The strengths and also the weaknesses are pretty well summarized in Stephen Colebourne (Joda-Time, JSR-310, etc) <a href="http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000737.html" rel="noreferrer">Enhanced for each loop iteration control</a> proposal to extend it in Java 7:</p> <blockquote> <h3>FEATURE SUMMARY:</h3> <p>Extends the Java 5 for-each loop to allow access to the loop index, whether this is the first or last iteration, and to remove the current item.</p> <h3>MAJOR ADVANTAGE</h3> <p>The for-each loop is almost certainly the most new popular feature from Java 5. <strong>It works because it increases the abstraction level - instead of having to express the low-level details of how to loop around a list or array (with an index or iterator), the developer simply states that they want to loop and the language takes care of the rest. However, all the benefit is lost as soon as the developer needs to access the index or to remove an item</strong>.</p> <p>The original Java 5 for each work took a relatively conservative stance on a number of issues aiming to tackle the 80% case. However, loops are such a common form in coding that the remaining 20% that was not tackled represents a significant body of code.</p> <p>The process of converting the loop back from the for each to be index or iterator based is painful. This is because <strong>the old loop style</strong> if significantly lower-level, <strong>is more verbose and less clear</strong>. It is also painful as most IDEs don't support this kind of 'de-refactoring'.</p> <h3>MAJOR BENEFIT:</h3> <p>A common coding idiom is expressed at a higher abstraction than at present. This aids <strong>readability and clarity</strong>.</p> <p>...</p> </blockquote> <p>To sum up, the enhanced for loop offers a concise higher level syntax to loop over a list or array which improves clarity and readability. However, it misses some parts: allowing to access the index loop or to remove an item.</p> <h3>See also</h3> <ul> <li><a href="http://blog.joda.org/2008/04/java-7-for-each-loop-control-access_1268.html" rel="noreferrer">Java 7 - For-each loop control access</a></li> <li><a href="https://docs.google.com/document/d/18C3rF7OCgUhZjzkuBJzfMzMk6uMY2NNio_SWlEQLzGo/pub" rel="noreferrer">Stephen Colebourne's original writeup</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload