title: Remove entries from the list using iterator
body: <p>I need to write a simple function that will delete all entries in the <code>List</code> that contains objects of the class <code>Elem</code>. I wrote the function <code>removeAllElements</code>, but it does not work if the size of the <code>List&lt;Elem&gt;</code> is greater than 1.</p>
  
  <pre><code>public class Test {
  
  public static void main(String[] args) {
          Work w = new Work();
          w.addElement(new Elem("a",new Integer[]{1,2,3}));
          w.addElement(new Elem("b",new Integer[]{4,5,6}));
  
          w.removeAllElements(); // It does not work for me.
      }
  }    
  
  public class Work {
  
      private List&lt;Elem&gt; elements = new ArrayList&lt;Elem&gt;();
  
      public void addElement(Elem e) {
          this.elements.add(e);
      }
  
      public void removeAllElements() {
          Iterator itr = this.elements.iterator(); 
          while(itr.hasNext()) {
              Object e = itr.next();
              this.elements.remove(e);
          }
      }
  
  }
  
  public class Elem {
  
      private String title;
      private Integer[] values;
  
      public Elem(String t,Integer v) {
          this.title = t;
          this.values = v;
      }
  
  }
  </code></pre>
  
  <p><strong>Edit#1</strong>
  The error message is the following:</p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
      at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
      at java.util.AbstractList$Itr.next(Unknown Source)
  </code></pre>