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

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -