Deep copy of ArrayList

G

Guest

Hi there

I have an ArrayList, and I'd wish to make a deep copy. So what I want to end up with is a new collection with the elements being a deep copy of the elements in the copied list

Can I use MemberWiseClone to do that

ArrayList a = new ArrayList()
// ... fill i
ArrayList b = a.MemberWiseClone()

Will be have elements that are clones of the elements in a? If not, how can I achieve this

Thanks
Tom Tempelaere
 
G

Guest

Hi TT,
Hi there,

I have an ArrayList, and I'd wish to make a deep copy. So what I want to end up with is a new collection with the elements being a deep copy of the elements in the copied list.

Can I use MemberWiseClone to do that?

ArrayList a = new ArrayList();
// ... fill it
ArrayList b = a.MemberWiseClone();

You can not use this line because *MemberwiseClone* method is
*protected*.
Will be have elements that are clones of the elements in a? If not, how can I achieve this?

If your ArrayList contains your classes then i can recomend
You to implement IClonable interfaces to your classes.
So You will have to implement your *Clone* methods that provide
*deep copy*.

I'm affraid You can not provide *deep copy* functionality for existent
classes without inheriting them. So *ArrayList* will does not make
its *deep copy* and you've got to do it in your code.

Regards

Marcin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top