kiedy wyjąć drugi ostatni element nie ma ConcurrentModificationExceptionJava ConcurrentModificationException
List<String> myList1 = new ArrayList<String>();
Collections.addAll(myList1, "str1","str2","str3","str4","str5");
for(String element : myList1){//no ConcurrentModificationException here
if(element.equalsIgnoreCase("str4"))
myList1.remove("str4");
}
System.out.println(myList1);
ale kiedy usunąć inne elementy jest ConcurrentModificationException
List<String> myList2 = new ArrayList<String>();
Collections.addAll(myList2, "str1","str2","str3","str4","str5");
for(String element : myList2){//ConcurrentModificationException here
if(element.equalsIgnoreCase("str1"))
myList2.remove("str1");
}
System.out.println(myList2);
co jest tego powodem?
Proszę uważnie przeczytać pytanie. – user1947415
W pierwszym kawałku kodu występuje również błąd "Wyjątek w wątku" główny "java.util.ConcurrentModificationException". Nie można aktualizować tej samej kolekcji podczas jej iterowania. –
Dlaczego nie dostałem wyjątku? Próbowałeś? – user1947415