In the following function I have declared local variables allPeopel
and itr
(they are overriding global variables). If I comment out the local variables (between the Astrixes below), then a ConcurrentModificationError is thrown. However, if I use local variables instead of global variables then the code works fine. I don't understand why this is the case? There are many other functions in the class so I'm trying to use global variables for more efficient code.
public void removeAPerson(){
int id;
Scanner sc = new Scanner(System.in);
System.out.print("Enter ID of person to delete > ");
id = sc.nextInt();
sc.nextLine();
System.out.println();
/*************************************/
ArrayList<Person> allPeople;
allPeople = Person.getAllPeople();
Iterator itr = allPeople.iterator();
/*************************************/
while(itr.hasNext()){
Person obj = (Person) itr.next();
int ID = obj.getID2();
if(ID == id){
itr.remove();
break;
}
}
}