tags:

views:

91

answers:

3

I studied Java is pass object reference by value, and in order to make a local copy of an object, I can either do clone() or copy-constructor. I also looked at deep/shallow copy as well as several post on stackoverflow.

Now I am looking at example:

List<String> list = new ArrayList<String>();
String one = "one"
list.add(one);

Few articles I read only mentions that ArrayList implements cloneable but does not really say how to make a local copy of "list" if type is List not ArrayList which does not implements cloneable.

I can call clone() if "list" is type of ArrayList.

ArrayList<String> list = new ArrayList<String();
list.clone();

But if type is List, I cannot.

Should I just use copy constructor like below to make local copy? What is the best way to make a copy of "list"?

List<String> tmpList = new ArrayList<String>(list);
+2  A: 

Passing the list into the constructor is probably the best way to go. The constructor invocation itself will use, behind the scenes, System.arraycopy. So it will effectively detach the local copy from the list passed in through the constructor.

John V.
+1  A: 

Use the copy constructor whenever you can. clone() is obsolete and should not be used (neither implemented) in new code.

If you extend a class that implements Cloneable, you have little choice but to implement a well-behaved clone method. Otherwise, you are better off providing an alternative means of object copying, or simply not providing the capability. [...] A fine approach to object copying is to provide a copy constructor or copy factory. [...]

Given all of the problems associated with Cloneable, it’s safe to say that other interfaces should not extend it, and that classes designed for inheritance (Item 17) should not implement it. Because of its many shortcomings, some expert programmers simply choose never to override the clone method and never to invoke it except, perhaps, to copy arrays.

From Effective Java 2nd Edition, Item 11.

Péter Török
A: 

This might work:

ArrayList<String> temp = new ArrayList<String>();
String one = "one";
temp.add(one);
String[] tempArr = temp.toArray(new String[0]);
List<String> temp2 = java.util.Arrays.asList((String[])tempArr.clone()); //This is an AbstractList: You don't want this.
List<String> list = new ArrayList<String>(temp2);
there you go!
Leo Izen