【发布时间】:2017-05-30 00:56:56
【问题描述】:
我有一个 Country 类,它保存一个由 Human 对象组成的列表。在 Country 类中,我具有以下功能;
public void processOneDay(int day, List<Country> countryList, int numberOfCountries ){
ListIterator<Human> iter = people.listIterator();
while(iter.hasNext()){
Human h = iter.next();
h.move(day, countryList, numberOfCountries);
}
}
Human 类的 move() 方法负责将人类从一个国家移动到另一个国家,但此方法将该人从源国家的人员列表中删除并将其添加到目的地国家的人员列表中。因此,在迭代时执行此操作会导致我出现 ConcurrentModificationException。我尝试使用迭代器本身的删除功能,但我把事情搞砸了。那么,我该如何处理这种情况?
【问题讨论】:
标签: arraylist iterator concurrentmodification