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;
    }
6
  • 2
    Where does tmp2 come from? Commented Sep 13, 2016 at 11:15
  • 3
    The list doesn't contain circles, it contains references to circles... Commented Sep 13, 2016 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. Commented Sep 13, 2016 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. Commented Sep 13, 2016 at 11:16
  • 1
    You're modifying the objects that are in the list. See this for a more detailed explanation. Commented Sep 13, 2016 at 11:18

2 Answers 2

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

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

Comments

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.

1 Comment

I'm sry. I was experementing on the Code. It is edited now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.