首先思考一个问题
list是一个ArrayList的对象,哪个选项的代码填到//todo delete处,可以在Iterator遍历的过程中正确并安全的删除一个list中保存的对象?()
1
2
3
4
5
6
7
8
9
10
11
|
Iterator
it = list.iterator(); int
index = 0 ; while
(it.hasNext()) { Object
obj = it.next(); if
(needDelete(obj)) //needDelete返回boolean,决定是否要删除 {
//todo
delete } index
++; } |
- it.remove();
- list.remove(obj);
- list.remove(index);
- list.remove(obj,index);
未完待续