嵌套迭代器

时间:2014-11-19 19:25:52

标签: java iterator

我正在尝试建立一个太空入侵者游戏并开发了这段代码,以便当其中一个人击中屏幕边缘时让所有外星人掉下来,但是我得到一个非常讨厌的错误信息当它遇到行Enemy de = downwardIterator.next();时。游戏仍然按预期运行,但是当它击中该行时,它会在后台触发这些错误消息。 这是代码:

Iterator<Enemy> iterator = enemyList.iterator();
    while (iterator.hasNext()) {
        Enemy e = iterator.next();

        if(e!=null && e.isActive()){
            e.move();
            e.draw(g);
            if(e.edgeHit()){
                Iterator<Enemy> downwardIterator = enemyList.iterator();
                while (iterator.hasNext()) {
                    Enemy de = downwardIterator.next();
                    de.dropPosition();
                    de.changeDirection();
                }
            }
        }
        else{
            iterator.remove();
        }
    }
只需让敌人左右移动即可。 e.edgeHit可以检测到任何敌人何时到达游戏画面的边缘。

当它到达该行时,这是完整的跟踪:

Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException
    at java.util.ArrayList$Itr.next(ArrayList.java:834)
    at GamePanel.paintComponent(GamePanel.java:96)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5219)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1529)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1452)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1249)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5167)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4978)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:808)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
    at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

2 个答案:

答案 0 :(得分:4)

你的第二个迭代器循环不正确。

Iterator<Enemy> downwardIterator = enemyList.iterator();
while (iterator.hasNext()) {
  // ...

应该是

Iterator<Enemy> downwardIterator = enemyList.iterator();
while (downwardIterator.hasNext()) {
  // ...

修改

您可能更喜欢for-each loop(使用隐藏的迭代器),如

for (Enemy de : enemyList) {
  // ...

答案 1 :(得分:2)

如果你想要一个解释,你要在你的upwardIterator对象上调用.next()方法,你的while循环不会检查正确的迭代器对象,看看是否还剩下任何元素:

Iterator<Enemy> downwardIterator = enemyList.iterator();
while (iterator.hasNext()) { // Check downwardIterator instead of iterator
    Enemy de = downwardIterator.next();
    de.dropPosition();
    de.changeDirection();
}

此外,当满足第一个if条件时,如果不修改敌人列表,则可以使用增强的for循环迭代你的敌人列表。