0

I have an ArrayList in my Method. It contains a special class of Circles. Within ColoredCircle there is a variable of type Point2D.Double p1. I have to transform these coordinates on a different coordinate system. When I do that the method is altering my ArrayList, filling it with the transformed coordinates. Why? I didn't even touch my ArrayList in my transformation method.

    public ArrayList<ColoredCircle> algorithm(final ArrayList<ColoredCircle> circleList) {


        ColoredCircle a = circleList.get(0);
        ColoredCircle b = circleList.get(1);
        ColoredCircle c = circleList.get(2);

        for (ColoredCircle cr : circleList) {
            System.out.println("algo"+ cr.getPoint().getX()+" "+cr.getPoint().getY());
        }

        Point2D.Double circleCoordinateC1 = this.transorm_coordinates_circle(b.center, a.getPoint());

        for (ColoredCircle cr : circleList) {
            System.out.println("algo2"+ cr.getPoint().getX()+" "+cr.getPoint().getY());
        }

Transormation Method:

    public Point2D.Double transorm_coordinates_circle(Point2D.Double circle_center, Point2D.Double point) {

        double x = point.getX() - circle_center.getX();
        double y = (point.getY() - circle_center.getY()) * -1;
        point.setLocation(x, y);

        System.out.println("Point "+point.getX()+" "+point.getY());

        return point;
    }
  • 2
    Where does `tmp2` come from? – Smutje Sep 13 '16 at 11:15
  • 3
    The list doesn't contain circles, it contains references to circles... – assylias Sep 13 '16 at 11:16
  • Why shouldn't it change? You are passing an object from the arraylist to a method. You are using the same object instance... Java is generally pass by reference and not pass by value. – M. Deinum Sep 13 '16 at 11:16
  • 1
    You don't touch your list, but you modify the items in it; how is it then surprising that you see the modified items? Look at your `transform_coordinates_circle` method. – fge Sep 13 '16 at 11:16
  • 1
    You're modifying the objects that are in the list. See [this](http://stackoverflow.com/questions/14346818/array-withother-classes-object-java/14347017#14347017) for a more detailed explanation. – m0skit0 Sep 13 '16 at 11:18
  • @M.Deinum actually Java is pass-by-value. Of course, in this case the value is a reference, so they're often confused. – Joeri Hendrickx Sep 13 '16 at 12:16

2 Answers2

2

Everything working as intended, the List handles objects by reference. You do not clone the circle objects, therefore the objects are manipulated.

the list only stores pointers to those objects.

google: call by reference, call by value and read on its details in java

Melv_80
  • 222
  • 1
  • 5
1

As it seems, your tmp2 is a reference into your circleList ArrayList. Since you don't change where it references, I assume you always see a specific one entry changing?

You'd have to show more context (specifically, where tmp2 is declared, and the place where you set it, before the call to "algorithm", to make sure.

codeling
  • 11,056
  • 4
  • 42
  • 71