1

So I have this piece of code:

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;
}

But for some reason whenever i excecute the line:

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

The previous "corners" values of the Triangle3D elements that are already in the list get overriden by the newle added ones. Which I think is weird because I clearly make a new Arraylist on this line:

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

I defined the Tuple class myself as following:

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);
    }

}

EDIT:

I found the root of the problem. But still don't know how to fix it. It's in my Triangle3D class. This is my 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;
        }
    }
}

The problem lies in this code in my combine function:

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

For some reason when I create this new Triangle3D my corner1, corner2, corner3 in the "new" creating triangle are already set (thus they are pointing to my already existing corners). How do I get rid of this dependency? I don't get it since i make new corners when I create that class.

6
  • An ArrayList allows duplicates. Are you sure your values are being replaced? Commented Mar 20, 2017 at 14:02
  • how are you calling the method ? Commented Mar 20, 2017 at 14:03
  • Not sure if this is related: If you're using that Tuple.copy method and expecting the two Tuples to have independent values, you're probably going to have your values copied over because you're only setting references. Commented Mar 20, 2017 at 14:06
  • @SteveSmith When i'm in debugging mode: the moment before the adding to the arraylist i can see in my variables list the current corners from other elements in the list. when i add the value all of the other cornervalues become the new one. Whilst the Triangle3D's in the returnTriangles list DO have different colors. So only the ArrayList<Point3D> are getting replaced Commented Mar 20, 2017 at 14:09
  • @dskfdskjgds the method is getting called like this: ArrayList<Triangle2D> leftTriangles = new ArrayList<Triangle2D>(this.findTriangles(left)); ArrayList<Triangle2D> rightTriangles = new ArrayList<Triangle2D>(this.findTriangles(right)); return this.combine(leftTriangles, rightTriangles); Commented Mar 20, 2017 at 14:09

1 Answer 1

1

Can you provide me with example data where I can see the value being overridden?

ArrayList.add add an element, it does not replace an element.

Your corners is within the for loop scope, so it shouldn't save any previous data.

I do not think you have debugged it properly and mistakenly think that it replaces the previous value. I would gladly help you, but I need the same data as the data that you have used.

Sign up to request clarification or add additional context in comments.

4 Comments

I added the root of the problem to my question, some data would just be some doubles for the corners and they just get overwritten. But I found out that when i debug in the creation of a Triangle3D when one already exists. That at the constructor my corner1,2 and 3 already have values
Remove static from your corner1, corner2, and corner3 in Triangle3D- private Point3d corner1 = new Point3d(); private Point3d corner2 = new Point3d(); private Point3d corner3 = new Point3d(); Always try to avoid using static. It should only be used for singular data that is static.
Yep, that was it o.O This fixed so much for me thanks!!
No problem. Just remember that static variable is a shared variable. It will keep its information until it is either set to null or a new value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.