title: Throwing ConcurrentModificationException if we add elements in ArrayList at the end while iterating
body: <p>I am iterating elements of ArrayList using Iteratot.While iterating I am adding an element into the ArrayList at the end. But getting an error : ConcurrentModificationException. </p>
  
  <p>If I change ArrayList to Set and tried to add elements in set at the end during iteration no error is throwing </p>
  
  <p>Test.java</p>
  
  <pre><code>class Test {
    public static void main() {
      ArrayList&lt;Integer&gt; al = new ArrayList&lt;Integer&gt;();
      al.add(20);
      al.add(30);
      al.add(40);
  
      Iterator&lt;Integer&gt; it = marks.iterator();
      while (it.hasNext()) {
        int value = it.next();
        if (value == 40) al.add(50);
      }
    }
  }
  </code></pre>
  
  <p>Test2.java</p>
  
  <pre><code>class Test2 {
    public static void main() {
      Set&lt;Integer&gt; marks = new HashSet&lt;&gt;();
      marks.add(20);
      marks.add(30);
      marks.add(40);
  
      Iterator&lt;Integer&gt; it = marks.iterator();
      while (it.hasNext()) {
        int value = it.next();
        if (value == 40) marks.add(50);
      }
    }
  }
  </code></pre>