title: Concurrent Modification Exception : adding to an ArrayList
body: <p>The problem occurs at </p>
  
  <pre><code>Element element = it.next();
  </code></pre>
  
  <p>And this code which contains that line, is inside of an <code>OnTouchEvent</code></p>
  
  <pre><code>for (Iterator&lt;Element&gt; it = mElements.iterator(); it.hasNext();){
      Element element = it.next();
  
      if(touchX &gt; element.mX  &amp;&amp; touchX &lt; element.mX + element.mBitmap.getWidth() &amp;&amp; touchY &gt; element.mY   
              &amp;&amp; touchY &lt; element.mY + element.mBitmap.getHeight()) {  
  
          //irrelevant stuff..
  
          if(element.cFlag){
              mElements.add(new Element("crack",getResources(), (int)touchX,(int)touchY));
              element.cFlag = false;
  
          }           
      }
  }
  </code></pre>
  
  <p>All of this is inside <code>synchronized(mElements)</code>, where <code>mElements</code> is an <code>ArrayList&lt;Element&gt;</code> </p>
  
  <p>When I touch an <code>Element</code>, it may activate <code>cFlag</code>, which will create another <code>Element</code> with different properties, which will fall off the screen and destroy itself in less than a second. It's my way of creating particle effects. We can call this "particle" <code>crack</code>, like the String parameter in the constructor.</p>
  
  <p>This all works fine until I add another main <code>Element</code>. Now I have two <code>Elements</code> on the screen at the same time, and if I touch the newest <code>Element</code>, it works fine, and launches the particles.</p>
  
  <p>However, if I touch and activate <code>cFlag</code> on the older <code>Element</code>, then it gives me the exception.</p>
  
  <pre><code> 07-28 15:36:59.815: ERROR/AndroidRuntime(4026): FATAL EXCEPTION: main
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026): java.util.ConcurrentModificationException
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at com.Juggle2.Panel.onTouchEvent(Panel.java:823)
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.view.View.dispatchTouchEvent(View.java:3766)
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1767)
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1119)
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1751)
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.os.Handler.dispatchMessage(Handler.java:99)
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.os.Looper.loop(Looper.java:123)
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.app.ActivityThread.main(ActivityThread.java:4627)
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at java.lang.reflect.Method.invokeNative(Native Method)
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at java.lang.reflect.Method.invoke(Method.java:521)
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
  07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>How can I make this work?</p>