I have a question connected with my GalaxyInvaders clone.
What is the difference between for and foreach iterating through ArrayList?
I'm modifying ArrayLists (changing variables / removing some objects) using foreach loops (I'm blocking sections before doing that using Semaphores).
Question is, when I use foreach statement, application throws exception.
Exception in thread "Thread-39" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at pl.mosek.MyPanel2$ColissionDetector.run(GUI.java:421)
at java.lang.Thread.run(Unknown Source)
When I use classic for statement, problem magically dissapears. 
Can somebody explain me why is that?
CODE:
@Override
    public void run() {
        while (true) {
            if (ship.getBullets().isEmpty() == false
                    && monsters.isEmpty() == false) {
                semaphoreLock(semaphoreBullet);
                for (Bullet i : ship.getBullets()) {
                    if (i.isDestroyed() != true) {
                        semaphoreLock(semaphoreMonster);
                        for (Enemy j : monsters) {
                            if (i.getBounds()
                                    .intersects(j.getBounds())
                                    &&j.isDestroyed() != true) {
                                i.setDestroyed(true);
                                i.setVisible(false);
                                j.setDestroyed(true);
                                j.setVisible(false);
                            }
                        }
                    }
                    semaphoreRelease(semaphoreMonster);
                }
                semaphoreRelease(semaphoreBullet);
            }
            try {
                Thread.sleep(25);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
If i switch foreach to for statments, everything working correctly.
 
     
     
    