【问题标题】:Why is my ArrayList not looping through all values?为什么我的 ArrayList 没有遍历所有值?
【发布时间】:2015-11-19 18:23:47
【问题描述】:

我正在尝试制作一个小应用程序,可以在窗口框架周围弹跳球。当我在列表中添加超过 1 个球时;它没有按预期循环它们。

这是我的代码:

主类:

public class Main extends JFrame {

private static final long serialVersionUID = 1L;
private List<Ball> balls = new ArrayList<Ball>();
private List<Ball> tempballs = new ArrayList<Ball>();
private static Timer t;

public void addBall(Ball b) {
    tempballs.add(b);
}

public void initUI() {
    this.setSize(500, 500);
    this.setDefaultCloseOperation(3);
    this.setVisible(true);
}

private BufferStrategy bs;
private Random r = new Random();
public void paint() {
    if (!tempballs.isEmpty()) {
        balls.addAll(tempballs);
        tempballs = new ArrayList<Ball>();
    }
    int i = 0;
    System.out.println(balls.size());
    for (Ball b : new ArrayList<Ball>(balls)) {
        i++;
        System.out.println(i);

        if ((bs = this.getBufferStrategy()) == null) {
            this.createBufferStrategy(2);
            return;
        }

        if (bs.contentsLost() || bs.contentsRestored()) {
            return;
        }
        if (b.y >= this.getHeight() - 100) {
            b.ydirection = -r.nextDouble() * 5;
        }
        if (b.y < 20) {
            b.ydirection = r.nextDouble() * 5;
        }
        if (b.x >= this.getWidth() - 100) {
            b.xdirection = -r.nextDouble() * 5;
        }
        if (b.x < 0) {
            b.xdirection = r.nextDouble() * 5;
        }

        b.x += b.xdirection;
        b.y += b.ydirection;

        if (b.xdirection > 0)
            b.xdirection += 0.1;
        else
            b.xdirection += -0.1;

        if (b.ydirection > 0)
            b.ydirection += 0.1;
        else
            b.ydirection += -0.1;

        Graphics g = bs.getDrawGraphics();

        g.fillOval((int) b.x, (int) b.y, 100, 100);

        bs.show();

        g.dispose();

        bs.dispose();

    }
    i = 0;

}

public static void main(String[] args) {
    try {
        final Main m = new Main();
        m.addMouseListener(new Mouse(m));
        m.initUI();
        t = new Timer();
        TimerTask tt = new TimerTask() {

            @Override
            public void run() {
                m.paint();
            }
        };
        t.schedule(tt, Calendar.getInstance().getTime(), 20);
    } catch (ConcurrentModificationException e) {
        e.printStackTrace();
    }
}

}

这是 Ball 类:

public class Ball {

private Random r = new Random();
public double y, x, ydirection, xdirection;
public Ball(int x, int y) {
    this.y = y;
    this.x = x;
    ydirection = r.nextGaussian() * 5;
    xdirection = r.nextGaussian() * 5;
}

}

和鼠标监听器:

public class Mouse implements MouseListener {
Main m;
public Mouse(Main m) {
    this.m = m;
}

@Override
public void mouseClicked(MouseEvent e) {
    m.addBall(new Ball(e.getX(), e.getY()));
    System.out.println("cl");

}
@Override
public void mouseEntered(MouseEvent arg0) {

}

@Override
public void mouseExited(MouseEvent arg0) {

}

@Override
public void mousePressed(MouseEvent arg0) {

}

@Override
public void mouseReleased(MouseEvent arg0) {

}

}

其他细节:

  • 它只遍历列表中的第一项,但列表大小会增加。
  • 我使用的是 java 6,但如果需要我会更改版本。

【问题讨论】:

  • 为什么要创建一个新的ArrayList&lt;Ball&gt; 作为balls 的副本?它不应该导致问题,但无论如何它很奇怪。
  • 你在重新粉刷吗?你如何检查球没有被环绕?
  • 这正是调试器的用途...没有人再教过如何调试了吗?
  • 我正在制作一个新的 ArrayList 作为球的副本以避免 ConcurrentModification 异常。

标签: java for-loop arraylist


【解决方案1】:

如果您的循环未按预期运行,请尝试找出它被取消的原因。您的循环中有两个 return 语句可能会导致此行为。在返回之前进行 sysouts 以找出原因。然后找出为什么你的 if 周围的回报是真的。要深入挖掘,您可以使用 IDE 的调试模式并在感兴趣的行处放置断点,或者使用步进模式一次运行一行代码。

除此之外,您可以将if 放在循环之前,当您在paint() 函数中时,它们正在检查的值不应更改(您正在使用可能会更改它们的 UI 线程)。

【讨论】:

  • 我听取了您的建议,将所有图形代码移到了循环之外。它成功了!我现在意识到我做错了什么。非常感谢!
【解决方案2】:

循环内有以下return 语句。如果执行到达任何return 语句,则循环结束。

弄清楚,如果您输入这些条件并返回列表中的第一个值。

if ((bs = this.getBufferStrategy()) == null) {
            this.createBufferStrategy(2);
            return;
        }

        if (bs.contentsLost() || bs.contentsRestored()) {
            return;
        }

【讨论】:

    猜你喜欢
    • 2020-11-25
    • 2020-07-15
    • 2020-04-12
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 2011-07-28
    • 2010-09-14
    • 1970-01-01
    相关资源
    最近更新 更多