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 - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -