标签:value 迭代 arraylist except pre util class iterator one
java.util.ConcurrentModificationException
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class ListOfRemove {
public static void main(String[] args) {
List<Integer> list = new ArrayList(Arrays.asList(1, 2, 10, 20, 30));
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
int x = it.next();
if (x >= 10) {
list.remove(Integer.valueOf(x));
}
}
System.out.println("Modified ArrayList : " + list);
}
}
应该使用 Iterator(迭代器) 的 remove() 方法删除
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class ListOfRemove {
public static void main(String[] args) {
List<Integer> list = new ArrayList(Arrays.asList(1, 2, 10, 20, 30));
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
int x = it.next();
if (x >= 10) {
it.remove();
}
}
System.out.println("Modified ArrayList : " + list);
}
}
Modified ArrayList : [1, 2]
java.util.ConcurrentModificationException
标签:value 迭代 arraylist except pre util class iterator one
原文地址:https://www.cnblogs.com/hglibin/p/11404207.html