title: Update ArrayList using another ArrayList in java
body: <p>I've come across a lot of information on ArrayLists and how to manipulate them but nothing seems to be answering my issue. </p>
  
  <p>I want to check if an element in an arraylist is not alive, and if so remove it but add another 2 to the list. Usually quite easy except that I need to then add the changes to another arraylist that contains all the elements in the first arraylist as well as elements from other external arraylists.</p>
  
  <p>I thought Id be able to do this using a temp arraylist as shown below:</p>
  
  <pre><code>import java.util.ArrayList;
  
  public class main {
  
      public static ArrayList&lt;String&gt; changedArr = new ArrayList(){ {add("M1"); add("alive"); add("M3");} };
      public static ArrayList&lt;String&gt; tempArr = new ArrayList();
      public static ArrayList&lt;String&gt; totalArr = new ArrayList(){ {add("M1"); add("alive"); add("M3"); add("L4"); add("S5");} };
  
      public static void main(String[] args) {
  
          System.out.println("changedArray = "+changedArr);
          System.out.println("tempArray = "+tempArr);
          System.out.println("totalArray = "+totalArr);
  
          for(Object a : changedArr){
              if(a !="alive") {
                  tempArr.clear();
                  changedArr.remove(a);
                  totalArr.remove(a);
                  tempArr.add("S6");
                  tempArr.add("S7");
                  changedArr.addAll(tempArr);
                  totalArr.addAll(tempArr);
              }
          }
          System.out.println("\nchangedArray = "+changedArr);
          System.out.println("tempArray = "+tempArr);
          System.out.println("totalArray = "+totalArr);
      }
  }
  </code></pre>
  
  <p>Where this code should return:</p>
  
  <pre><code>changedArray = [M1, alive, M3]
  tempArray = []
  totalArray = [M1, alive, M3, L4, S5]
  
  changedArray = [alive, S6, S7]
  tempArray = [S6, S7]
  totalArray = [alive, L4, S5, S6, S7]
  </code></pre>
  
  <p>It's instead returning:</p>
  
  <pre><code>Exception in thread "main" java.util.ConcurrentModificationException
  changedArray = [M1, M2, M3]
      at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
  tempArray = []
      at java.util.ArrayList$Itr.next(ArrayList.java:851)
  totalArray = [M1, M2, M3, L4, S5]
      at main.main(main.java:31)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
  
  Process finished with exit code 1
  </code></pre>
  
  <p>So my question is, what am I doing wrong to cause these errors? Is this method possible? If not, I don't see why, could you explain? And how could I get around it?</p>
  
  <p>If you've made it this far, thanks for taking the time to read my ramblings! :D</p>