Iterator的remove删除的是调用next方法后,指针指向的元素。
remove方法必须在next方法执行后调用。(猜测:remove后,指针不再指向元素)。
Collection coll = new ArrayList();
coll.add(123);
coll.add("abc");
coll.add(456);
Iterator iterator = coll.iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
if ("abc".equals(obj)) {
iterator.remove();
// iterator.remove();//调用两次会报错
}
System.out.println(obj);
}