gpt4 book ai didi

java - 如何循环遍历数组列表以查看是否有任何对象具有相同的值

转载 作者:太空宇宙 更新时间:2023-11-04 09:55:51 26 4
gpt4 key购买 nike

我有一个汽车数组列表,我想循环遍历这个数组列表,看看两辆车是否处于完全相同的位置,这样我就可以看看它们是否发生了碰撞。我写了以下内容,但我得到的只是“没有碰撞”,即使它们发生了碰撞。我用两种方法来实现。我的假设是,由于两个循环都从同一点循环,它们是否只是不断地一起检查同一辆车或类似的东西?那么 if (i != collided) 每次都会被触发?如果是这样我该如何阻止?

public void carCollision(Car collided) {

for (Car i: cars) {
if(i != collided && i.getLane() == collided.getLane() &&
i.getPosition() == collided.getPosition()) {
System.out.println("collision");
} else {
System.out.println("no collisions");
}
}
}

public void check() {
for (Car a: cars) {
carCollision(a);
}
}

汽车类别 -

/** State of a car on the road */
public class Car {

/** Position of this car on the road (i.e. how far down the road it is) in pixels */
private double position;
/** Current speed in pixels per second */
private double speed;
/** Lane that this car is on */
private int lane;
/** Colour of this car's display */
private Color color;

public Car(double position, double speed, int lane, Color color) {
this.position = position;
this.speed = speed;
this.lane = lane;
this.color = color;
}

/** @return a new Car object with the same state as this one */
public Car clone() {
return new Car(position, speed, lane, color);
}

/** Update this car after `elapsed' seconds have passed */
public void tick(Environment environment, double elapsed) {
position += speed * elapsed;
}

public double getPosition() {
return position;
}

public int getLane() {
return lane;
}

public Color getColor() {
return color;
}

这是我的主类,用于展示我如何调用该方法,我使用 e.check();在 addcars 方法中 -

public class Main extends Application {
public static void main(String[] args) {
launch(args);
}

public void start(Stage stage) {

final Environment environment = new Environment();
final Display display = new Display(environment);
environment.setDisplay(display);

VBox box = new VBox();

stage.setTitle("Traffic");
stage.setScene(new Scene(box, 800, 600));

HBox controls = new HBox();
Button restart = new Button("Restart");
controls.getChildren().addAll(restart);
box.getChildren().add(controls);

restart.setOnMouseClicked(e -> {
environment.clear();
display.reset();
addCars(environment);
});

box.getChildren().add(display);

addCars(environment);

stage.show();
}

/** Add the required cars to an environment.
* @param e Environment to use.
*/
private static void addCars(Environment e) {
/* Add an `interesting' set of cars */
Random r = new Random();
e.add(new Car( 0, 63, 2, new Color(r.nextFloat(), r.nextFloat(), r.nextFloat(), 1.0)));
e.add(new Car( 48, 79, 0, new Color(r.nextFloat(), r.nextFloat(), r.nextFloat(), 1.0)));
e.add(new Car(144, 60, 0, new Color(r.nextFloat(), r.nextFloat(), r.nextFloat(), 1.0)));
e.add(new Car(192, 74, 0, new Color(r.nextFloat(), r.nextFloat(), r.nextFloat(), 1.0)));
e.add(new Car(240, 12, 1, new Color(r.nextFloat(), r.nextFloat(), r.nextFloat(), 1.0)));
e.add(new Car(288, 77, 0, new Color(r.nextFloat(), r.nextFloat(), r.nextFloat(), 1.0)));
e.add(new Car(336, 28, 1, new Color(r.nextFloat(), r.nextFloat(), r.nextFloat(), 1.0)));
e.add(new Car(384, 32, 2, new Color(r.nextFloat(), r.nextFloat(), r.nextFloat(), 1.0)));
e.add(new Car(432, 16, 1, new Color(r.nextFloat(), r.nextFloat(), r.nextFloat(), 1.0)));
e.check();
}
};

更新以包括我的环境类,现在问题很啰嗦,但我觉得问题可能出在我如何使用环境类?

public class Environment implements Cloneable {

/** All the cars that are on our road */
private ArrayList<Car> cars = new ArrayList<Car>();
/** The Display object that we are working with */
private Display display;
/** Number of lanes to have on the road */
private int lanes = 4;
private long last;

/** Set the Display object that we are working with.
*/
public void setDisplay(Display display) {
this.display = display;

/* Start a timer to update things */
new AnimationTimer() {
public void handle(long now) {
if (last == 0) {
last = now;
}

/* Update the model */
tick((now - last) * 1e-9);

/* Update the view */
double furthest = 0;
for (Car i: cars) {
if (i.getPosition() > furthest) {
furthest = i.getPosition();
}
}
display.setEnd((int) furthest);
display.draw();
last = now;
}
}.start();
}

/** Return a copy of this environment */
public Environment clone() {
Environment c = new Environment();
for (Car i: cars) {
c.cars.add(i.clone());
}
return c;
}

/** Draw the current state of the environment on our display */
public void draw() {
for (Car i: cars) {
display.car((int) i.getPosition(), i.getLane(), i.getColor());
}
}

/** Add a car to the environment.
* @param car Car to add.
*/
public void add(Car car) {
cars.add(car);
}

public void clear() {
cars.clear();
}

/** @return length of each car (in pixels) */
public double carLength() {
return 40;
}

/** Update the state of the environment after some short time has passed */
private void tick(double elapsed) {
Environment before = Environment.this.clone();
for (Car i: cars) {
i.tick(before, elapsed);
}
}

/** @param behind A car.
* @return The next car in front of @ref behind in the same lane, or null if there is nothing in front on the same lane.
*/
public Car nextCar(Car behind) {
Car closest = null;
for (Car i: cars) {
if (i != behind && i.getLane() == behind.getLane() && i.getPosition() > behind.getPosition() && (closest == null || i.getPosition() < closest.getPosition())) {
closest = i;
}
}
return closest;
}

public void carCollision(Car collided) {

for (Car i: cars) {
double MIN_DIS = 0.1;
if(!(i.equals(collided)) && i.getLane() == collided.getLane() &&
(Math.abs(i.getPosition() - collided.getPosition()) < MIN_DIS )) {
System.out.println("collision");
} else {
System.out.println("no collisions");
}
}
}

public void check() {
for (Car a: cars) {
carCollision(a);
}

}

public void speed() {
for (Car a : cars) {
a.setSpeed();
}
}

/** @return Number of lanes */
public int getLanes() {
return lanes;
}

}

更新 - 尚未修复,但我认为我已经接近了。我使用“nextCar”方法添加了以下代码 -

public Car nextCar(Car behind) {
Car closest = null;
for (Car i: cars) {
if (i != behind && i.getLane() == behind.getLane() && i.getPosition() > behind.getPosition() && (closest == null || i.getPosition() < closest.getPosition())) {
closest = i;
}
}
return closest;
}

public void collision() {
Environment e = Environment.this.clone();
double MIN_DIS = 0.5;
for (Car i : cars) {
e.nextCar(i);
for (Car a : cars) {
if(!(i.equals(a)) && i.getLane() == a.getLane() &&
(Math.abs(i.getPosition() - a.getPosition()) < MIN_DIS)) {
System.out.println("collision");
} else {
System.out.println("no collision");
}

System.out.println("closest car is" + i);
}
}
}

这设法打印出最近的汽车,所以我知道它有点工作,尽管它仍然不会检测到碰撞?知道可能是什么问题吗?我在 main 的 addCars 方法中使用 e.collision() 来调用它

最佳答案

您是否对 cars 中的每辆车调用 check() ?您发布的代码未显示您如何使用 check()

此外,你还写了

two cars are in the exact same position

但必须提醒您,使用浮点位置,这确实很棘手。如果两辆车具有相同的初始位置、速度,并且使用相同的 elapsed 参数调用它们的 tick,那么它们将具有相同的位置。但是,在任何其他情况下,由于舍入误差,它们的位置可能会相差很小的分数,例如 0.00000000001

您必须向我们展示一个包含一组汽车的完整示例,以及如何在它们上调用 check()

关于java - 如何循环遍历数组列表以查看是否有任何对象具有相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54170987/

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