删除了arraylists的indexOutOfBoundsException

时间:2015-01-28 01:17:53

标签: java arraylist indexoutofboundsexception

我正在写一个空间入侵者在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.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.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));
    }
}

请帮忙

1 个答案:

答案 0 :(得分:2)

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