所以我有这样一段代码:的Java添加到ArrayList的替代其它值
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;
}
但出于某种原因,每当我excecute行:
returnTriangles.add(new Triangle3D(corners, eL.getColor()));
先前的“角落”的价值已经在列表中的Triangle3D元素被newle添加的元素覆盖。我认为这是奇怪,因为我清楚地辨认这条线新的ArrayList:
ArrayList<Point3d> corners = new ArrayList<Point3d>(3);
我自己定义的元组类如下:
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创建三角形已经设置(因此他们指向我已经存在的角落)。我如何摆脱这种依赖性?我没有得到它,因为当我创建这个类时,我创造了新的角落。
ArrayList允许重复。你确定你的价值观正在被取代吗? –
你打算怎么调用这个方法? – dskfdskjgds
不确定这是否相关:如果您使用'Tuple.copy'方法并期望两个'Tuple'具有独立值,那么您可能会将您的值复制过来,因为您只设置引用。 – Zircon