| Author |
ArrayList clone question
|
thirun pavan
Greenhorn
Joined: Jun 27, 2007
Posts: 22
|
|
Hi all,
I am trying to use the ArrayList clone() method as follows
But I always get an error in the line:
List<String> test2 = test.clone(); //Error here
So can someone please tell me what am I doing wrong here?
I am just trying to clone the 'test' object and use the returned object to create a new ArrayList
Thanks in advance for your time
Regards
|
 |
Rusty Shackleford
Ranch Hand
Joined: Jan 03, 2006
Posts: 490
|
|
Object.clone() is not implemented in List. From the Object.clone() docs:
The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time. The clone method is implemented by the class Object as a convenient, general utility for subclasses that implement the interface Cloneable, possibly also overriding the clone method, in which case the overriding definition can refer to this utility definition by the call:
Since you have List reference, you can't directly call it from ArrayList. One of the problems of using a base class reference. If you know your program will need an ArrayList and not another class that implements List, just use an ArrayList reference. clone() also returns an Object, so casting will be necessary.
Also, clone() returns a shallow copy. Are you sure that is what you want?
|
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
Methods exist in the base class List that allow you to add a collection to another list.
try this:
hope this helps.
Hunter.
|
"If the facts don't fit the theory, get new facts" --Albert Einstein
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3041
|
|
Rusty Shackleford wrote:Object.clone() is not implemented in List.
...
Since you have List reference, you can't directly call it from ArrayList. One of the problems of using a base class reference. If you know your program will need an ArrayList and not another class that implements List, just use an ArrayList reference. clone() also returns an Object, so casting will be necessary.
Also, clone() returns a shallow copy. Are you sure that is what you want?
And since the clone() makes a shallow copy, and base-type reference doesn't implement clonable, you should consider using ArrayList's copy constructor, which will be easier to use, and less limiting in what reference types it works on:
|
Steve
|
 |
thirun pavan
Greenhorn
Joined: Jun 27, 2007
Posts: 22
|
|
Rusty Shackleford wrote:Object.clone() is not implemented in List. From the Object.clone() docs:
The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time. The clone method is implemented by the class Object as a convenient, general utility for subclasses that implement the interface Cloneable, possibly also overriding the clone method, in which case the overriding definition can refer to this utility definition by the call:
Since you have List reference, you can't directly call it from ArrayList. One of the problems of using a base class reference. If you know your program will need an ArrayList and not another class that implements List, just use an ArrayList reference. clone() also returns an Object, so casting will be necessary.
Also, clone() returns a shallow copy. Are you sure that is what you want?
Hi Steve,
Thanks for taking the time to reply to my message. Actually I am not writing any 'specific' program, I am just training myself on java collections, so I was visiting http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html#clone()
and I thought of trying all of the ArrayList methods, from which was clone()
Back to the ArrayList and clone method, I followed your recommendation and changed List to ArrayList in test1 and test2, but still I got the following error when trying to compile:
so I casted to ArrayList as follows:
but after compiling I still get:
Note: TestArrayList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
but when I tried to run the class everything went just fine, so I was just wondering is it possible to compile without getting any notes? or in other words why I am getting those 2 notes, and how to resolve them 'from code'?
Thanks in advance for your time
|
 |
thirun pavan
Greenhorn
Joined: Jun 27, 2007
Posts: 22
|
|
Hunter, Steve,
Thanks for taking the time to reply to my message...My main idea was not to just add the content of test in test2, my idea mainly was to test the usage of clone() method
|
 |
Rusty Shackleford
Ranch Hand
Joined: Jan 03, 2006
Posts: 490
|
|
What is the type of the reference?
ArrayList<String> You should cast it to this.
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
Casting to an arraylist wont fix the problem, you get a compile time error because, at compile time the jvm only sees the declaration of the type, it trusts the programmer to instantiate correctly. And there is no clone() method in the List api.
Note: I like Steve's solution to this problem better than mine because he uses methods from the Arraylist class instead of the generic List class
Hunter
|
 |
Rusty Shackleford
Ranch Hand
Joined: Jan 03, 2006
Posts: 490
|
|
Hunter McMillen wrote:Casting to an arraylist wont fix the problem, you get a compile time error because, at compile time the jvm only sees the declaration of the type, it trusts the programmer to instantiate correctly. And there is no clone() method in the List api.
Note: I like Steve's solution to this problem better than mine because he uses methods from the Arraylist class instead of the generic List class
Hunter
That is not an error, but a warning. It will still compile. Generics is an ugly hack at times, putting @SuppressWarnings("unchecked") at the top of the method will shut the compiler up, if clone() really is needed, which I suspect it is not.
|
 |
James Basller
Ranch Hand
Joined: Sep 07, 2008
Posts: 58
|
|
Hi,
I think below code will resolve your problem. As arrayList provides clone() method.
Thanks!!!
|
 |
 |
|
|
subject: ArrayList clone question
|
|
|