I've come across a lot of information on ArrayLists and how to manipulate them but nothing seems to be answering my issue.
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.
I thought Id be able to do this using a temp arraylist as shown below:
import java.util.ArrayList;
public class main {
public static ArrayList<String> changedArr = new ArrayList(){ {add("M1"); add("alive"); add("M3");} };
public static ArrayList<String> tempArr = new ArrayList();
public static ArrayList<String> 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);
}
}
Where this code should return:
changedArray = [M1, alive, M3]
tempArray = []
totalArray = [M1, alive, M3, L4, S5]
changedArray = [alive, S6, S7]
tempArray = [S6, S7]
totalArray = [alive, L4, S5, S6, S7]
It's instead returning:
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
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?
If you've made it this far, thanks for taking the time to read my ramblings! :D
You are modifying your ArrayList changedArr while iterating over it using an enhanced for loop. This causes a ConcurrentModificationException to be thrown because enhanced for loops produce iterators for the looping to occur, and iterators assume that the list will not me modified during the iterator's lifetime. If you modify a list while an iterator is iterating over it then you will get a ConcurrentModificationException.
A solution would be to have an original list and a copy and only modify the copy. You should never modify a list while you are iterating over it.