ArrayList and Cloning the Objects inside the ArrayList

I am having two lists say

ArrayList list1,list2

list2 is initiallised as

list2=new ArrayList(list1);

or

list2=list2.addAll(list1);

But when i made any changes in the object in list1 then it is reflected in list2 because same reference.

But i dont need this. I need the lists to be independent of each other.

One routine i know is to add the clone of the objects in list1 to list2. This need some job to be done, such as implementing Cloneable interface and loopings.

Is there any other smartest way to do this?

Regards VELU

[627 byte] By [Er.Vela] at [2007-11-27 0:15:14]
# 1
ArrayList implements interface Cloneable and offer the method clone(). So you could use clone() method to obtain a copy (indipendent from the original) of your ArrayList:[code](ArrayList) list2=list1.clone();HiDiego
DiegoCarzanigaa at 2007-7-11 22:01:51 > top of Java-index,Java Essentials,New To Java...
# 2
Diego , I ThankYou for u kind reply.But i don't want to clone the ArrayList. But I need the elements in the original list to get cloned and copied to the Other list.
Er.Vela at 2007-7-11 22:01:51 > top of Java-index,Java Essentials,New To Java...
# 3
People of Java Forum please Think Of this.
Er.Vela at 2007-7-11 22:01:51 > top of Java-index,Java Essentials,New To Java...
# 4

Implement clone so that it creates a second List that holds clones of each element, not just copies of the references to each element. Then use the addAll method to add the contents of that newly cloned List to the other list. It'll be easiest to implement clone in the objects you store in the list as well, if it's your own class. That way the list clone method can call the element's clone method to get a copy, and add it to the clone list.

hunter9000a at 2007-7-11 22:01:51 > top of Java-index,Java Essentials,New To Java...
# 5
@hunter FOR Implement clone so that it creates a second List that holds clones of each element, not just copies of the references to each element.Where to implement Clone?
Er.Vela at 2007-7-11 22:01:51 > top of Java-index,Java Essentials,New To Java...