修改 Arraylist 中的某个元素

Modifying a certain element in a Array List

如何参考第一个球的 x 位置并将其用在第二个球上?

private List<Ball> createBalls() {
    List<Ball> balls = new ArrayList<>();

    balls.add(new Ball(Color.BLUE, 600, 400, 20, rInt(-15,15), rInt(-15,15)));
    balls.add(new Ball(Color.RED, ball(1).get position 1, 500, 13, rInt(-15,15), rInt(-15,15)));
    balls.add(new Ball(Color.GREEN, 320, 340, 20, rInt(-15,15), rInt(-15,15)));
    balls.add(new Ball(Color.YELLOW, 50, 400, 50, rInt(-15,15), rInt(-15,15)));
    balls.add(new Ball(Color.orange, 600, 300, 15, rInt(-15,15), rInt(-15,15)));
    balls.add(new Ball(Color.CYAN, 320, 340, 10, rInt(-15,15), rInt(-15,15)));
    balls.add(new Ball(Color.GRAY, 50, 400, 60, rInt(-15,15), rInt(-15,15)));
    balls.add(new Ball(Color.magenta, 320, 340, 25, rInt(-15,15), rInt(-15,15)));
    balls.add(new Ball(Color.white, 50, 400, 40, rInt(-15,15), rInt(-15,15)));
    balls.add(new Ball(Color.YELLOW, 800, 200, 500, rInt(-15,15), rInt(-15,15)));

    return balls;
}

在我的球里class我有

    public double getPositionX() {
        return positionX;
    }

    public void setPositionX(double positionX) {
        this.positionX = positionX;
    }

不确定我是否理解但要获得 X 的值(假设有一个 Ball.getX()。 然后:balls.get(1).getX();

"for the next element position value?"

您无法在创建之前获取 "next element"。因此,在通过列表创建 Balls 循环并应用 "get next X value" 逻辑之后。

如果您显示更多代码并更清楚地解释您要做什么,将会有所帮助。