gpt4 book ai didi

java - 修改数组列表中的某个元素

转载 作者:行者123 更新时间:2023-12-01 09:25:20 24 4
gpt4 key购买 nike

如何引用第一个球的 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;
}

在我的球课上,我有

    public double getPositionX() {
return positionX;
}

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

最佳答案

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

“下一个元素位置值?”

在创建“下一个元素”之前,您无法获取它。因此,在创建 Balls 后,循环遍历列表并应用“获取下一个 X 值”逻辑。

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

关于java - 修改数组列表中的某个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39880751/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com