I'm quite new to java and I'm still trying to figuring out how arrays work here. The following code, which I thought would print "2":
import java.util.ArrayList;
public static void main(String[] args) {
ArrayList<String> myArray = new ArrayList<String>();
myArray.add("1");
myArray.add("2");
myArray.add("1");
myArray.add("1");
for (String s: myArray){
if(s == "1")
myArray.remove(s);
}
for (String s: myArray) {
System.out.println(s);
}
}
Throws this exception when executing:
Exception in thread "main" java.util.ConcurrentModificationException
at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1042)
at java.base/java.util.ArrayList$Itr.next(ArrayList.java:996)
at ex.main(ex.java:40)
As I said this might be a stupid question, but my unexperienced eyes just can't see what is happening here.
Thank you all in advance.