title: Java: Best way to iterate through a Collection (here ArrayList)
body: <p>Today I was happily coding away when I got to a piece of code I already used hundreds of times:</p>
  
  <blockquote>
    <p>Iterating through a Collection (here ArrayList)</p>
  </blockquote>
  
  <p>For some reason, I actually looked at the autocompletion options of Eclipse and it got me wondering:</p>
  
  <p><strong>What cases are the following loops better to use than the others?</strong></p>
  
  <p>The classic array index loop:</p>
  
  <pre><code>for (int i = 0; i &lt; collection.length; i++) {
    type array_element = collection.get(index);
  }
  </code></pre>
  
  <p>The Iterator hasNext()/next():</p>
  
  <pre><code>for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
    type type = (type) iterator.next();   
  }
  </code></pre>
  
  <p>And my favorite because its so simple to write:     </p>
  
  <pre><code>for (iterable_type iterable_element : collection) {
  
  }
  </code></pre>