gpt4 book ai didi

java - 为什么我的 ArrayList 没有循环遍历所有值?

转载 作者:行者123 更新时间:2023-11-30 03:09:57 25 4
gpt4 key购买 nike

我正在尝试制作一个小应用程序,可以在窗口框架周围弹跳球。当我向列表中添加超过 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,但如果需要,我会更改版本。

最佳答案

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

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

关于java - 为什么我的 ArrayList 没有循环遍历所有值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33811560/

25 4 0