I have an ArrayList filled with objects "Project" called projectsCurrent. Each project has its name, state, manDays etc.
I wanna iterate through this list and on each project check if (manDaysDone >= manDays). If it does, than the project is done and I wanna change its state, remove it from the list and add it to another one (projectsDone).
When I try to use Iterator to achieve my goal, it seems like the link to the current project is lost inside the iteration, for example, in this code, an NoSuchElementException is raised inside the if condition:
Iterator<Project> i = projectsCurrent.iterator();
while (i.hasNext()){
if (i.next().getManDaysDone() >= i.next().getManDays()){
i.next().setState(ProjectState.DONE);
}
}
Just to make clear what I want to achieve. The following for loop does the job, but of course later I get the ConcurrentModificationException:
for (Project currentProject : projectsCurrent){
if (currentProject.getManDaysDone() >= currentProject.getManDays()) {
currentProject.setState(ProjectState.DONE);
projectsDone.add(currentProject);
this.budget += currentProject.getPrice();
projectsCurrent.remove(currentProject);
}
}
currentprojectjust update that object