1

I know I can create an object this way

int[] list1 = {1, 2}; int[] list2 = list1.clone(); 

and this normally works. But why doesn't this work properly:

ArrayList<Double> list1 = new ArrayList<Double>(); list1.add(1.0); list1.add(2.0); list1.add(0.5); ArrayList<Double> list2 = list1.clone(); 

What I know is that this code is fine

ArrayList<Double> list2 = (ArrayList<Double>)list1.clone(); 

maybe because list1.clone() is doesn't return a reference type, so it needs (ArrayList) to make it return a reference type.

but why int[] list2 = list1.clone(); can work?

2
  • 1
    What do you mean by it doesn't work properly? Commented Jan 29, 2015 at 1:44
  • it indicates that Multiple markers at this line - Type mismatch: cannot convert from Object to ArrayList<Double> Commented Jan 29, 2015 at 1:56

3 Answers 3

4

ArrayList's clone() method does a shallow copy, which you can read about here.

Consider using a copy constructor instead, new ArrayList(listToCopy). Something like this:

ArrayList<Double> list1 = new ArrayList<Double>(); list1.add(1.0); list1.add(2.0); list1.add(0.5); ArrayList<Double> list2 = new ArrayList<Double>(list1); 

As to why what you tried to do the first time didn't work, the clone() method returns an Object type, so you need to cast it to a ArrayList<Double> before you can initialize another ArrayList with it.

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

Comments

1

You can refer to this post, there are some useful answers there. Deep copy, shallow copy, clone

In short, clone() only copies an object at 1 level (meaning shallow copy) while deep copy could copy an object at more than 1 level. You can find an article about deep clone here. Deep Clone It's a guide to build your own deep clone function.

Comments

0

In response to your new question, why does the int[] cloning work, it is because when clone() runs over an int[], all it sees are primitive types, and as such, simply returns the reference to the primitive type (which happens to be, you guessed it, an int[])

see: http://howtodoinjava.com/2012/11/08/a-guide-to-object-cloning-in-java/

2 Comments

thank you a lot, this explains the int[]. but from the website you give me, it says "If the class contains members of any class type then only the object references to those members are copied and hence the member references in both the original object as well as the cloned object refer to the same object." .Does it mean list2 in ArrayList<Double> has the same reference as list1? but after I add "list2.add(3.5);", list1 doesn't change.
The references in the object haven't changed though; the 1.0, 2.0, and 0.5 inside list1 are still the same object as the 3 of those inside list2; it just so happens to be that adding a new object to list2 won't add the same object to list1. If list 1 had a variable x = 2.0, then list 2 would also have x = 2.0, and if you changed x in either one of them, they would both change.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.