• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList clone question

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Methods exist in the base class List that allow you to add a collection to another list.

try this:



hope this helps.
Hunter.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
thirun pavan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the type of the reference?

ArrayList<String> You should cast it to this.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think below code will resolve your problem. As arrayList provides clone() method.



Thanks!!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic