Skip to content

Instantly share code, notes, and snippets.

@mahata
Created January 1, 2019 14:35
Show Gist options
  • Save mahata/0a4600822d345bb1a14a73c18a592863 to your computer and use it in GitHub Desktop.
Save mahata/0a4600822d345bb1a14a73c18a592863 to your computer and use it in GitHub Desktop.
How remove() works 2
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
for (String str : list) {
if ("C".equals(str)) {
list.remove(str);
}
}
for (String str : list) {
System.out.println(str);
}
}
}
/*
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 Main.main(Main.java:11)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment