在集合中移除或者添加元素
https://stackoverflow.com/questions/10431981/remove-elements-from-collection-while-iterating
这是大致思路:
AFAIK, there are two approaches:
Iterate over a copy of the collection
Use the iterator of the actual collection
For instance,
关于不同语言有不同的实现形式:
List<Foo> fooListCopy = new ArrayList<Foo>(fooList);
for(Foo foo : fooListCopy){
// modify actual fooList
}
and
Iterator<Foo> itr = fooList.iterator();
while(itr.hasNext()){
// modify actual fooList using itr.remove()
}