简体   繁体   中英

I'm getting an IndexOutOfBoundsException with my arraylist, what am I doing wrong?

This class is in a program for a game I'm writing that is basically Space Invaders. I'm getting Exceptions for some reason and I don't see why I should be getting them.

Here's the class in question, but I can post all the code if necessary:

public class GamePanel extends JPanel {

    Launcher launcher1;
    Background bground1;
    Shot shot;
    public ArrayList<Shot> shots;
    public int numShots;
    public static int counter;

    public GamePanel() throws IOException {
        super();
        this.shots = new ArrayList<>();
        this.numShots = 0;
        launcher1 = new Launcher();
        bground1 = new Background();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(bground1.background, 0, 0, getWidth(), getHeight(), null);
        g.drawImage(launcher1.baldEagleImage, launcher1.getLxCoord(), launcher1.lyCoord, null);//paint the launcher
        while (counter == 1) {
            for (int i = 0; i < shots.size(); i++) {
                g.drawImage(shots.get(i).mcDShotImage, shots.get(i).staticXLauncherCoord, shots.get(i).getSyCoord(), null);
            }
        }
    }

    public void move(GamePanel gamePanel) {
        launcher1.moveX();
        if (numShots > 0) {
            moveShot();
        }
        repaint();
    }

    public void moveShot() {
        for (int i = 0; i < numShots; i++) {//loop to move all the shots
            if (shots.get(i).getSyCoord() > 10) { // THIS IS THE ISSUE, but I don't know why
                counter = 1;
                shots.get(i).moveY();
                repaint();
            } else {
                counter = 0;
                shots.remove(i);
                repaint();
            }
        }
    }

    public void createShots() {
        try {
            for (int j = 0; j < numShots; j++) {
                shots.add(new Shot());
            }
        } catch (IOException | IndexOutOfBoundsException e) {
            System.out.println("caught an exception" + e);
        }
    }
}

The problem is in this piece of code:

} else {
    counter = 0;
    shots.remove(i);
    repaint();
}

You remove an item from shots without adjusting numShots , causing an index out of bounds exception in one of subsequent iterations.

To fix this, either add numShots-- in the else branch, or use the built-in size() method that returns the count of elements in a list instead: unlike numShots which you need to maintain, shots.size() never gets "out of sync" with the actual count.

在上面的代码行中,很明显的问题是numShots是> shots.size()(因为您还删除了镜头。由于我无法在您增加numShots的位置,因此在这段代码中,一个简单的(尽管不是请从逻辑角度确定),如下更改您的for循环:

for (int i = 0; i < shots.size(); i++)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM