title: In Java, can you modify a List while iterating through it?
body: <p>I understand that in Java a <code>Collection&lt;E&gt;</code> should not be modified while iterating through it, such as removing or adding elements. But what about changing the elements in a List? For example, what if we have</p>
  
  <pre><code>List&lt;String&gt; letters = new ArrayList&lt;String&gt;();
  letters.add("A");
  letters.add("B");
  letters.add("C");
  int i = 0;
  
  for (String letter : letters) {
      letters.set(i, "D");
      i++;
  }
  </code></pre>
  
  <p>So, I'm not talking about modifying the object stored at an element; I'm talking about changing <i>what</i> the object is. The size of the List is not being changed, but the object at the index is changing, so technically the List is being modified. My boss claims this code is fine (and it does appear to work), but I still don't think it is correct. Would a different way of doing it, maybe using the set(E e) method of a ListIterator, be better? </p>