gpt4 book ai didi

Java 添加到 arraylist 会覆盖其他值

转载 作者:行者123 更新时间:2023-11-30 06:50:29 25 4
gpt4 key购买 nike

所以我有这段代码:

private ArrayList<Triangle3D> combine(ArrayList<Triangle2D> leftTriangles, ArrayList<Triangle2D> rightTriangles) {
ArrayList<Triangle3D> returnTriangles = new ArrayList<Triangle3D>();
for (Triangle2D eL : leftTriangles){
Triangle2D eR = eL.getOtherTriangle(rightTriangles);
if(eR != null){
ArrayList<Point3d> corners = new ArrayList<Point3d>(3);
for(Tuple<Integer, Integer> cornerL : eL.getCorners()){
Tuple<Integer, Integer> cornerR = eR.getCorrespondingCorner(cornerL);
if(cornerL != cornerR){
corners.add(addDistances(cornerL, cornerR));
}
}
returnTriangles.add(new Triangle3D(corners, eL.getColor()));
}
}
return returnTriangles;
}

但由于某种原因,每当我执行该行时:

        returnTriangles.add(new Triangle3D(corners, eL.getColor()));

列表中已有的 Triangle3D 元素的先前“角”值将被新添加的元素覆盖。我认为这很奇怪,因为我显然在这一行创建了一个新的数组列表:

        ArrayList<Point3d> corners = new ArrayList<Point3d>(3);

我自己定义了 Tuple 类,如下所示:

package vision.polyhedradetection;

public class Tuple<X, Y> {
private final X x;
private final Y y;
public Tuple(X x, Y y) {
this.x = x;
this.y = y;
}

public X getX() { return x; }
public Y getY() { return y; }

public Tuple<X, Y> copy(){
return new Tuple<X,Y>(this.x, this.y);
}


@Override
public String toString() {
return "(" + x + "," + y + ")";
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

if (!(other instanceof Tuple)){
return false;
}

Tuple<Integer, Integer> other_ = (Tuple<Integer, Integer>) other;

return other_.x == (this.x) && other_.y == (this.y);
}

}

编辑:

我找到了问题的根源。但还是不知道如何解决。它在我的 Triangle3D 类(class)中。这是我的课:

package vision.polyhedradetection;

import javax.vecmath.Point3d;
import java.util.List;

public class Triangle3D {
private static Point3d corner1 = new Point3d();
private static Point3d corner2 = new Point3d();
private static Point3d corner3 = new Point3d();
private int color;

public Triangle3D(Point3d corner1, Point3d corner2, Point3d corner3, int color) {
this.corner1 = corner1;
this.corner2 = corner2;
this.corner3 = corner3;
this.color = color;
}

public Triangle3D(List<Point3d> corners, int color) {
this.corner1 = corners.get(0);
this.corner2 = corners.get(1);
this.corner3 = corners.get(2);
this.color = color;
}

private static Point3d centroid;

public Triangle3D(Point3d centroid, int color) {
this.centroid = centroid;
this.color = color;
}

public Point3d[] getCorners() {
Point3d[] Corners = {corner1, corner2, corner3};
return Corners;
}

public int getColor() {
return this.color;
}

public Point3d getCentroid() {
if (this.centroid != null) return this.centroid;
else {
Double x = (double) Math.round((corner1.x + corner2.x + corner3.x) / 3);
Double y = (double) Math.round((corner1.y + corner2.y + corner3.y) / 3);
Double z = (double) Math.round((corner1.z + corner2.z + corner3.z) / 3);
this.centroid = new Point3d(x, y, z);
return this.centroid;
}
}
}

问题出在我的组合函数中的这段代码:

        returnTriangles.add(new Triangle3D(corners, eL.getColor()));

出于某种原因,当我创建这个新的 Triangle3D 时,"new"创建三角形中的corner1、corner2、corner3 已经设置(因此它们指向我已经存在的角)。我该如何摆脱这种依赖?我不明白,因为我在创建该类时创建了新的角落。

最佳答案

您能否向我提供示例数据,让我可以看到值被覆盖?

ArrayList.add添加一个元素,它不会替换一个元素。

你的角点在 for 循环范围内,所以它不应该保存任何以前的数据。

我认为您没有正确调试它并错误地认为它替换了以前的值。我很乐意帮助您,但我需要与您使用的数据相同的数据。

关于Java 添加到 arraylist 会覆盖其他值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42905596/

25 4 0