2017-03-20 19 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