I'm trying to make an applet of Asteroid objects that float across the screen. If two asteroids collide then the one with the lesser speed is supposed to break up into two smaller asteroids. Once an asteroid is size 1 it should vanish.
When I try to compare two asteroids I get a ConcurrentModificationException and I'm not sure why.
private volatile Collection<Asteroid> belt;
private void handleCollisions() {
Collection<Asteroid> psuedoBelt = belt;
Iterator<Asteroid> one;
Iterator<Asteroid> two;
for (one = psuedoBelt.iterator(); one.hasNext();) {
Asteroid aOne = one.next();
for (two = psuedoBelt.iterator(); two.hasNext();) {
Asteroid aTwo = two.next();
if (aOne.collidesWith(aTwo)) {
if (aOne.getSpeed() > aTwo.getSpeed()) {
Collection<Asteroid> split = aTwo.split();
two.remove();
for (Iterator<Asteroid> three = split.iterator(); three
.hasNext();) {
psuedoBelt.add(three.next());
}
} else {
Collection<Asteroid> split = aOne.split();
one.remove();
for (Iterator<Asteroid> three = split.iterator(); three
.hasNext();) {
psuedoBelt.add(three.next());
}
}
}
}
}
belt = psuedoBelt;
}