concurrentmodification - Avoiding ConcurrentModificationException when using nested for-loops in Java -


in program i've created teams(football example) , want create method every team plays match against other teams. method throws concurrentmodificationexception. here is:

public void playmatches(){     (team team : mteams) {         mteams.remove(team);         (team team1 : mteams) {             match(team, team1);         }         mteams.add(team);     } } 

i'm removing team mteams, doesn't play against itself, throws exception. how can handle that?

since seem understand what's breaking, let's consider "how handle" part of question. news not need modify collection @ all.

rather modifying collection removing , adding item, skip team outer loop when call match in inner loop, this:

for (team team : mteams) {     (team team1 : mteams) {         if (team.equals(team1)) continue;         match(team, team1);     } } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -