删除数组列表的 indexOutOfBoundsException

indexOutOfBoundsException with arraylists deletion

我正在写一个 space 入侵者在 java 中结束,我几乎完成了实际的工作部分,但我不断收到这个错误:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.remove(ArrayList.java:492)
at GamePanel.shotDelete(SpaceInvaders.java:261)
at GamePanel.shieldHit(SpaceInvaders.java:198)
at SpaceInvaders.actionPerformed(SpaceInvaders.java:44)
at javax.swing.Timer.fireActionPerformed(Timer.java:313)
at javax.swing.Timer$DoPostEvent.run(Timer.java:245)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:697)
at java.awt.EventQueue.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我知道以前有人问过这个问题,但没有人想解决它。我抛出这个错误的代码是:

public void shieldHit(){
    //enemy shots
    ArrayList<Integer>usedBadShot=new ArrayList<Integer>();
    for(int b=0;b<badShotList.size();b++){
        for(int i=0;i<shieldShape.length;i++){
            for(int j=0;j<shieldShape[i].length;j++){
                if(shieldShape[i][j]==1){
                    int xc=shieldLocX+j*6;
                    int yc=375+i*6;
                    if(badShotList.get(b)[x]<=xc+6&&badShotList.get(b)[x]>=xc&&badShotList.get(b)[y]<=yc+6&&badShotList.get(b)[y]>=yc){
                        //delete hit block
                        shieldShape[i][j]=0;

                        //randomly delete left or right of spot
                        int randint=(int)(Math.random()*101);
                        if(randint<=33){
                            shieldShape[i][j+1]=0;
                        }
                        else if(randint>33&&randint<67){
                            shieldShape[i][j-1]=0;
                        }
                        usedBadShot.add(b);
                    }
                }
            }
        }   
    }
    for(int i=0;i<usedBadShot.size();i++){
        badShotDelete(usedBadShot.get(i));
    }
    //user shots
    ArrayList<Integer>usedShot=new ArrayList<Integer>();
    for(int b=0;b<shotList.size();b++){
        for(int i=0;i<shieldShape.length;i++){
            for(int j=0;j<shieldShape[i].length;j++){
                if(shieldShape[i][j]==1){
                    int xc=shieldLocX+j*6;
                    int yc=375+i*6;
                    if(shotList.get(b)[x]<=xc+6&&shotList.get(b)[x]>=xc&&shotList.get(b)[y]<=yc+6&&shotList.get(b)[y]>=yc){
                        //delete hit block
                        shieldShape[i][j]=0;

                        //randomly delete left or right of spot
                        int randint=(int)(Math.random()*101);
                        if(randint<=33){
                            shieldShape[i][j+1]=0;
                        }
                        else if(randint>33&&randint<67){
                            shieldShape[i][j-1]=0;
                        }
                        usedShot.add(b);
                    }
                }
            }
        }   
    }
    for(int i=0;i<usedShot.size();i++){
        shotDelete(usedShot.get(i));
    }
}

请帮忙

ArrayList 中删除元素时,最好从头到尾进行。这样,for循环中使用的索引将不会进入因删除数据而无法再使用的索引。