title: Why is using the for-each loop on an ArrayList not working?
body: <p>I've <a href="http://www.programcreek.com/2014/05/top-10-mistakes-java-developers-make/" rel="nofollow">stumbled upon this code</a>, which throws a <code>ConcurrentModificationException</code></p>
  
  <pre><code>ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;(Arrays.asList("a", "b", "c", "d"));
  
  for (String s : list) {
      if (s.equals("a"))
          list.remove(s);
  }
  </code></pre>
  
  <p>If you add an Iterator and use a while-loop, the code works fine:</p>
  
  <pre><code>ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;(Arrays.asList("a", "b", "c", "d"));
  Iterator&lt;String&gt; iter = list.iterator();
  while (iter.hasNext()) {
      String s = iter.next();
  
      if (s.equals("a")) {
          iter.remove();
      }
  }
  </code></pre>
  
  <p>I dont understand, why it is necessary, to use <code>Iterator&lt;String&gt;</code> in this case. 
  Is it because of the ArrayList not having some sort of ability to get iterated, altough it is a <a href="https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html" rel="nofollow">subclass of Collection</a>?</p>
  
  <p>Is it necessary to use the while-loop, or could you produce a version with a for-loop?</p>