Lưu ý: Tôi nhận thức được Iterator#remove()phương pháp.
Trong mẫu mã sau đây, tôi không hiểu tại sao phương thức List.removetrong mainném ConcurrentModificationException, nhưng không phải trong removephương thức.
public class RemoveListElementDemo {
private static final List<Integer> integerList;
static {
integerList = new ArrayList<Integer>();
integerList.add(1);
integerList.add(2);
integerList.add(3);
}
public static void remove(Integer toRemove) {
for(Integer integer : integerList) {
if(integer.equals(toRemove)) {
integerList.remove(integer);
}
}
}
public static void main(String... args) {
remove(Integer.valueOf(2));
Integer toRemove = Integer.valueOf(3);
for(Integer integer : integerList) {
if(integer.equals(toRemove)) {
integerList.remove(integer);
}
}
}
}
ConcurrentModificationExceptionkia thì không.
return;vòng lặp.
Iterator#remove(). Tại sao bạn làm theo cách này?