所以我有这段代码:
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元素的先前“corner”值被newle添加的值覆盖。我认为这很奇怪,因为我明确地在这一行上创建了一个新的Arraylist:
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课程中。这是我的班级:
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时,我的corner1,corner2,corner3在“new”创建三角形中已经设置(因此它们指向我已经存在的角落)。我如何摆脱这种依赖?我没有得到它,因为我在创建该课程时创造新的角落。
答案 0 :(得分:1)
您可以向我提供示例数据,我可以看到值被覆盖吗?
ArrayList.add添加一个元素,它不会替换元素。
您的角落位于for循环范围内,因此不应保存以前的任何数据。
我认为您没有正确调试它并错误地认为它取代了以前的值。我很乐意帮助您,但我需要与您使用的数据相同的数据。