Created
January 1, 2019 14:35
-
-
Save mahata/0a4600822d345bb1a14a73c18a592863 to your computer and use it in GitHub Desktop.
How remove() works 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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