2017-03-20 13 views
1

所以我有这样一段代码:的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创建三角形已经设置(因此他们指向我已经存在的角落)。我如何摆脱这种依赖性?我没有得到它,因为当我创建这个类时,我创造了新的角落。

+0

ArrayList允许重复。你确定你的价值观正在被取代吗? –

+0

你打算怎么调用这个方法? – dskfdskjgds

+0

不确定这是否相关:如果您使用'Tuple.copy'方法并期望两个'Tuple'具有独立值,那么您可能会将您的值复制过来,因为您只设置引用。 – Zircon

回答

1

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

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

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

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

+0

我将问题的根源添加到我的问题中,有些数据只是 一些双角的角落,他们只是被覆盖。但是我发现当我在创建Triangle3D时调试已经存在的时候。在构造函数中,我的角1,2和3已经有值 – Enforcerke

+0

从Triangle3D中的角1,角2和角3中移除静态-1,'私人Point3d corner1 =新Point3d();' '私人Point3d corner2 =新Point3d(); ' '私人Point3d corner3 =新Point3d();' 总是尽量避免使用静态。它只能用于静态的奇异数据。 – kkflf

+0

是的,就是这样o.O这固定了我多谢了! – Enforcerke