title: Java ConcurrentModificationException when iterating ArrayList
body: <p>I get <code>ConcurrentModificationException</code> when iterating an <code>ArrayList</code> and adding objects to a secondary <code>ArrayList</code>. I don't really know why because I'm not editing the list that I'm iterating through.</p>
  
  <p>This happens in two parts of my code. These are the codes.</p>
  
  <p><strong>EDIT -</strong> Code 1:</p>
  
  <pre><code>public static ConcurrentHashMap&lt;Long, ArrayList&lt;HistoricalIndex&gt;&gt; historicalIndexesMap = new ConcurrentHashMap&lt;Long, ArrayList&lt;HistoricalIndex&gt;&gt;();
  
  ArrayList&lt;HistoricalIndex&gt; historicalIndexList = IndexService.historicalIndexesMap.get(id);
  List&lt;Double&gt; tmpList = new ArrayList&lt;Double&gt;();
  for(HistoricalIndex hi : historicalIndexList){ //EXCEPTION HERE
      if((System.currentTimeMillis()-hi.getTimestamp()) &gt;= ONE_MINUTE){
          tmpList.add(hi.getIndex());
      }
  }
  </code></pre>
  
  <p>In Code 1 above, should I copy the historicalIndexList like this:</p>
  
  <pre><code>ArrayList&lt;HistoricalIndex&gt; historicalIndexList = new ArrayList&lt;HistoricalIndex&gt;(IndexService.historicalIndexesMap.get(id));
  </code></pre>
  
  <p>instead of doing this: ?</p>
  
  <pre><code>ArrayList&lt;HistoricalIndex&gt; historicalIndexList = IndexService.historicalIndexesMap.get(id);
  </code></pre>
  
  <p>Code 2:</p>
  
  <pre><code>List&lt;Double&gt; tmpList = new ArrayList&lt;Double&gt;();
  for(HistoricalIndex hi : list){ //EXCEPTION HERE
      tmpList.add(hi.getIndex());
  }
  </code></pre>
  
  <p>Does anyone have a clue why this happens? </p>
  
  <p>Stacktrace:</p>
  
  <pre><code>21:19:50,426 ERROR [stderr] (pool-9-thread-6) java.util.ConcurrentModificationException
  21:19:50,429 ERROR [stderr] (pool-9-thread-6)   at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
  21:19:50,432 ERROR [stderr] (pool-9-thread-6)   at java.util.ArrayList$Itr.next(ArrayList.java:831
  </code></pre>