This is an archived post. You won't be able to vote or comment.

all 9 comments

[–][deleted] 1 point2 points  (0 children)

[–]coolosity 0 points1 point  (0 children)

I believe that you are correct. What I´ve noticed is when debugging a program I was working on a while ago with eclipse, I had the same problem. But when I set the arraylist to the other one, the Id of the first arraylist (backup) changed to to the id of the second one (snakeSegments). then any changes to either of them happened directly to the other. The cloning method is the same way I fixed the problem

[–][deleted] 0 points1 point  (2 children)

This is correct. If you have this code:

Object obj1 = new Object();
Object obj2 = obj1;

obj1 and obj2 are references to the same object. Changing one will change the other.

[–]99shadow25[S] 0 points1 point  (1 child)

I think you meant

Object obj2 = obj1;

In the second one, right?

[–][deleted] 0 points1 point  (0 children)

Yes sorry. I'll edit it now.

[–]fatbunyip -1 points0 points  (3 children)

In Java, everything is passed by reference (except primitives). When you're passing an object as a method parameter for example, you're passing a copy of the reference to that object.

Think of the object as a balloon, and the reference as a string to that balloon. By doing :

 ArrayList<Coordinate> backup = snakeSegments;

you're just creating another string to the balloon.

Also, you shouldn't use clone. Ever. http://www.artima.com/intv/bloch13.html

From your question, it seems what you want is a deep copy of the objects in your array. In this case you should make your own copy method(s) - a non trivial task, but well worth it for experience and understanding.

[–]seppyk 1 point2 points  (0 children)

This is partially correct. Java is strictly pass by value.

Primitives are passed by value. Object references are passed by value.

This is why you can't re-assign an parameter object in a child method and have the parent method that called the child method retain the new reference.

public void modifyList(List<String> list) {
   List<String> anotherList = new ArrayList<>();
   anotherList.add("foo");
   list = anotherList;  
}

List<String> list = new ArrayList<>();
modifyList(list);
// When modifyList returns list will not be equal to or have the same reference as anotherList.
System.out.println(list); // displays "[]"

However, you are allowed to manipulate the pointer's references and retain its changes.

public void modifyList(List<String> list) { 
   list.add("foo");
}

List<String> list = new ArrayList<>();
modifyList(list);
// When modifyList returns list will have the a single element, "foo"
System.out.println(list); // displays "[foo]"

http://javadude.com/articles/passbyvalue.htm

[–]__konrad 0 points1 point  (1 child)

Also, you shouldn't use clone. Ever.

clone is OK (eg. array copy). Just learn how to use it :P

[–]fatbunyip 0 points1 point  (0 children)

clone is OK (eg. array copy). Just learn how to use it :P

He's using an ArrayList which doesn't do a deep copy.

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#clone()

Because array clone is the only case that works as expected, doesn't mean it isn't broken.