I have a Country class which keeps a list that consisted of Human objects. And in Country class I have following function;
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);
}
}
The move() method of Human class is responsible for movement of a Human from one country to other but this method is removing that person from source country's person list and add it to destination country's people list. So, doing this operation while iterating causes me ConcurrentModificationException. I tried to use remove function of iterator itself but I messed up things more.So, how can I handle this situation?