代码:
public static void main(String[] args) {
List<String> test = new ArrayList<String>();
test.add("aaa");
test.add("bbb");
test.add("ccc");
Iterator it = test.iterator();
Iterator it2 = test.iterator();
// List<String> list = IteratorUtils.toList(it);
// System.out.println(list.toString());
while(it.hasNext()){
String next = (String) it.next();
System.out.println(next);
if(next.equals("aaa")){
it.remove();
}
// it.remove();
}
System.out.println(test);
// List<String> list = IteratorUtils.toList(it);
// System.out.println(list.toString());
while(it.hasNext()){
System.out.println(it.next());
}
}
结果:
aaa
bbb
ccc
[bbb, ccc]
迭代器remove元素后,list集合的元素也会随着删除