Apa ConcurrentModificationException ing Jawa?
Tuladha
Ayo goleki conto carane pengecualian iki kedadeyan.import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
//Test driver for ConcurrentModificationException
public class ConcurrentModificationExceptionTestDriver1 {
public static void main(String[] args) {
List<String> businessDays = new ArrayList<>();
businessDays.add("Monday");
businessDays.add("Tuesday");
businessDays.add("Wednesday");
businessDays.add("Thursday");
businessDays.add("Friday");
businessDays.add("Saturday");
businessDays.add("Sunday");
Iterator<String> iterator = businessDays.iterator();
while (iterator.hasNext()) {
String day = iterator.next();
if (day.equals("Sunday")) {
// removing a list item while iterating
// leading to java.util.ConcurrentModificationException
businessDays.remove(day);
}
}
}
}
Output
Pangecualian ing thread "utama" java.util.ConcurrentModificationException ing java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1042) ing java.base/java.util.ArrayList$Itr.next(ArrayList.java :996) ing milestone.thirty.seven.ConcurrentModificationExceptionTestDriver1.main(ConcurrentModificationExceptionTestDriver1.java:22)
Kepiye Cara Ngindhari ConcurrentModificationException ing Jawa?
Ana macem-macem solusi kanggo ConcurrentModificationException . Ing kene ana sawetara. Sampeyan bisa milih sapa wae miturut kabutuhan sampeyan.- Aja modifikasi nalika iterasi.
- Gunakake puteran tradisional kanggo pengulangan lan modifikasi simultan.
- Gawe salinan nilai sing bakal diowahi nalika nggunakake iterator. Nganyari nilai yen pengulangan wis rampung.
Tuladha
Punika conto carane sampeyan bisa ngindhari ConcurrentModificationException .import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
//Test driver for ConcurrentModificationException
public class ConcurrentModificationExceptionTestDriver2 {
public static void main(String[] args) {
List<String> businessDays = new ArrayList<>();
businessDays.add("Monday");
businessDays.add("Tuesday");
businessDays.add("Wednesday");
businessDays.add("Thursday");
businessDays.add("Friday");
businessDays.add("Saturday");
businessDays.add("Sunday");
System.out.println("Initial Business Days: " + businessDays);
// Method II - Using traditional for loop
for (int i = 0; i < businessDays.size(); i++) {
if (businessDays.get(i).equals("Sunday")) {
businessDays.remove(i);
}
}
Iterator<String> iterator = businessDays.iterator();
List<String> businessDaysToBeRemoved = new ArrayList<>();
// Method III - Creating a copy of elements
while (iterator.hasNext()) {
String day = iterator.next();
if (day.equals("Saturday")) {
// Add values to be removed in the list
businessDaysToBeRemoved.add(day);
}
}
businessDays.removeAll(businessDaysToBeRemoved);
System.out.println("Final Business Days: " + businessDays);
}
}
Output
Dina Kerja Awal: [Senin, Selasa, Rebo, Kamis, Jum'at, Setu, Minggu] Dina Kerja Akhir: [Senin, Selasa, Rebo, Kemis, Jum'at]
GO TO FULL VERSION