title: What leads to ConcurrentModificationException
body: <p>I was reading about ConcurrentModificationException. I found this piece of code for the iterator.Can anybody explain what actually leads to this exception.I just want some justification for the logic written below.</p>
  
  <pre><code>public boolean hasNext() {
      return cursor != size;
  }
  
  @SuppressWarnings("unchecked")
  public E next() {
      checkForComodification();
      int i = cursor;
      if (i &gt;= size)
          throw new NoSuchElementException();
      Object[] elementData = ArrayList.this.elementData;
      if (i &gt;= elementData.length)
          throw new ConcurrentModificationException();
      cursor = i + 1;
      return (E) elementData[lastRet = i];
  }
  
  public void remove() {
      if (lastRet &lt; 0)
          throw new IllegalStateException();
      checkForComodification();
  
      try {
          ArrayList.this.remove(lastRet);
          cursor = lastRet;
          lastRet = -1;
          expectedModCount = modCount;
      } catch (IndexOutOfBoundsException ex) {
          throw new ConcurrentModificationException();
      }
  }
  
  final void checkForComodification() {
      if (modCount != expectedModCount)
          throw new ConcurrentModificationException();
      }
  }
  </code></pre>