范例1:
package com.contoso;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class MainApp {
/**
* 这是错误的ArrayList删除范例,条件判断里删除的是第2个元素执行失败,
*/
public static void test1() {
List<String> list = new ArrayList<>();
list.add("shanghai");
list.add("wuhan");
System.out.println("before remove list = " + list);
for (String item : list) {
if ("wuhan".equals(item)) {
list.remove(item);
}
}
System.out.println("after remove list = " + list);
}
/**
* 这是错误的ArrayList删除范例,条件判断里删除的是第1个元素能执行成功,不稳定的代码依然是错误的实现方式
*/
public static void test2() {
List<String> list = new ArrayList<>();
list.add("shanghai");
list.add("wuhan");
System.out.println("before remove list = " + list);
for (String item : list) {
if ("shanghai".equals(item)) {
list.remove(item);
}
}
System.out.println("after remove list = " + list);
}
/**
* 这是错误的ArrayList删除范例
*/
public static void test3() {
List<String> list = new ArrayList<>();
list.add("shanghai");
list.add("wuhan");
System.out.println("before remove list = " + list);
Iterator it = list.iterator();
while (it.hasNext()) {
String item = (String) it.next();
if ("wuhan".equals(item)) {
list.remove(item);
}
}
System.out.println("after remove list = " + list);
}
/**
* 这是正确的ArrayList删除范例
*/
public static void test4() {
List<String> list = new CopyOnWriteArrayList<>();
list.add("shanghai");
list.add("wuhan");
System.out.println("before remove list = " + list);
for (String item : list) {
if ("wuhan".equals(item)) {
list.remove(item);
}
}
System.out.println("after remove list = " + list);
}
/**
* 这是正确的ArrayList删除范例
*/
public static void test5() {
List<String> list = new ArrayList<>();
list.add("shanghai");
list.add("wuhan");
System.out.println("before remove list = " + list);
Iterator it = list.iterator();
while (it.hasNext()) {
String item = (String) it.next();
if ("wuhan".equals(item)) {
it.remove();
}
}
System.out.println("after remove list = " + list);
}
/**
* 这是正确的ArrayList删除范例
*/
public static void test6() {
List<String> list = new ArrayList<>();
list.add("shanghai");
list.add("wuhan");
System.out.println("before remove list = " + list);
list.removeIf("wuhan"::equals);
System.out.println("after remove list = " + list);
}
public static void main(String[] args) {
test1();
}
}
test1() method Console output :
run:
before remove list = [shanghai, wuhan]
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
at java.util.ArrayList$Itr.next(ArrayList.java:859)
at com.contoso.MainApp.test1(MainApp.java:18)
at com.contoso.MainApp.main(MainApp.java:105)
C:\Users\ZiGoo\AppData\Local\NetBeans\Cache\10.0\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\ZiGoo\AppData\Local\NetBeans\Cache\10.0\executor-snippets\run.xml:94: Java returned: 1
BUILD FAILED (total time: 5 seconds)
test2() method Console output ,though it run successfully, it 's wrong code:
run:
before remove list = [shanghai, wuhan]
after remove list = [wuhan]
BUILD SUCCESSFUL (total time: 7 seconds)
test3() method Console output :
run:
before remove list = [shanghai, wuhan]
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
at java.util.ArrayList$Itr.next(ArrayList.java:859)
at com.contoso.MainApp.test3(MainApp.java:51)
at com.contoso.MainApp.main(MainApp.java:105)
C:\Users\ZiGoo\AppData\Local\NetBeans\Cache\10.0\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\ZiGoo\AppData\Local\NetBeans\Cache\10.0\executor-snippets\run.xml:94: Java returned: 1
BUILD FAILED (total time: 0 seconds)
test4() method Console output :
run:
before remove list = [shanghai, wuhan]
after remove list = [shanghai]
BUILD SUCCESSFUL (total time: 0 seconds)
test5() method Console output :
run:
before remove list = [shanghai, wuhan]
after remove list = [shanghai]
BUILD SUCCESSFUL (total time: 0 seconds)
test6() method Console output :
run:
before remove list = [shanghai, wuhan]
after remove list = [shanghai]
BUILD SUCCESSFUL (total time: 0 seconds)