How to correctly animate images in a data structure and not get ConcurrentModificationException

狂风中的少年 提交于 2019-12-02 03:31:40

You can't remove an item from a List from within a for-each loop. I don't know the details, but I know it generally doesn't work.

Instead, get a Iterator of the List and use it's remove method instead...

Iterator<Fireball> it = fireBalls.iterator();
while (it.hasNext()) {
    Fireball ball = it.next();
    if (ball.x > D_W) {
        // You can't call this.  The Iterator is backed by the ArrayList
        //fireBalls.remove(ball);
        it.remove();
    } else {
        ball.x += X_INC;
        repaint();
    }
}

Happy fire ball spamming!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!