ArrayList Copy (Deep)

  • Thread starter Thread starter Desmond Cassidy
  • Start date Start date
D

Desmond Cassidy

Hi,
I'm sure this has been asked several times before but I'll risk it ;-)

If I wish to save an Arraylist to another Arraylist and work on te
original without affecting the contents of the new or saved Arraylist, how
is it done. It always seems to create a shallow copy i.e. by Reference.
I even tried passing the original Arraylist to a function by Value and
return a copy....after an arraylist.clear on the original it still clears
the saved Arraylist.

Thus the code I would like would be similar to the following

larrlstArrayListSave = larrlstArrayListOriginal
larrlstArrayListOriginal.Clear
BUT the larrlstArrayListSave should still have the collection in there.

I looked at .Clone and .CopyTo but they don't seem to cut !!!

It's annoying the hell out of me !!!

Any ideas ???

Cheers,

Desmond
 
Desmond Cassidy said:
Hi,
I'm sure this has been asked several times before but I'll risk it ;-)

If I wish to save an Arraylist to another Arraylist and work on te
original without affecting the contents of the new or saved Arraylist, how
is it done. It always seems to create a shallow copy i.e. by Reference.
I even tried passing the original Arraylist to a function by Value and
return a copy....after an arraylist.clear on the original it still clears
the saved Arraylist.

Thus the code I would like would be similar to the following

larrlstArrayListSave = larrlstArrayListOriginal
larrlstArrayListOriginal.Clear
BUT the larrlstArrayListSave should still have the collection in
there.

I looked at .Clone and .CopyTo but they don't seem to cut !!!

It's annoying the hell out of me !!!

Any ideas ???

Cheers,

Desmond
Clone will do it if the elements in your ArrayList are primitives, but if
you're storing reference types in the ArrayList, it looks like you'll have
to do the necessary work of cloning each of the actual objects in your own
"DeepCopy" method.
I'm a bit confused by your example, however, as Clear() simply sets the
ArrayList Count property to zero, thus the references established in the
second ArrayList still point to the objects not deleted by Clear().
There may be a better solution/technique that I've missed.
 
OK...I found something written about this a few weeks ago...and it seems to
have caused some confusion.

CopyTo and Clone seem to be the buzz words.

In my case I 'should' use Clone as this creates a DEEP copy whereas
CopyTo will NOT work with an Arraylist.
CopyTo will copy an Arraylist to an Array Type and NOT an Arraylist.

Thus, and I would like the gurus in you to confirm this.....

A Shallow copy is
larrlstArrayListSave = larrlstArrayListOriginal
A Deep Copy is
larrlstArrayListSave =
DirectCast(larrlstArrayListOriginal.Clone(),ArrayList)

Cheers,

Desmond.
 
Hi Peter,
Thanks for replying.....

I am storing plain old strings and it seems to work OK...so I should
STILL watch it if the type is a strong reference type ??

Cheers,

Desmond.
 
Desmond.

You have to make a deepCopy as Peter already wrote. I never found a standard
answer how to make a deepCopy on MSDN.

Problem is that it is probably impossible because of the fact that an
arraylist holds references to objects, wich can be everything. In fact they
can all be different, not very usefull, however it is possible.

Do you have any idea how to copy the content of this arraylist without
knowing what it is. I don't for me it looks impossible (I as well don't know
how to use it when I don't keep track what every reference per index holds).
\\\
Dim ar As New ArrayList
ar.Add(New Integer)
ar.Add(New Double)
Dim str As String
ar.Add(str)
Dim arl2 As New ArrayList
Dim arl3 As New ArrayList
arl2.Add(arl3)
ar.Add(arl2)
///
Before you say this is crazy, I try only to show you what it can be.

However serializing and deserializing can be a good workaround for this
problem.

\\\
Private Function SerializeArraylist(ByVal _
arraylst As ArrayList) As String
Dim bf As New
Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim mem As New IO.MemoryStream
bf.Serialize(mem, arraylst)
Return Convert.ToBase64String(mem.ToArray())
End Function
Private Function DeserializeArraylist(ByVal _
arraystring As String) As ArrayList
Dim bf As New
Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim mem As New
IO.MemoryStream(Convert.FromBase64String(arraystring))
Return DirectCast(bf.Deserialize(mem), ArrayList)
End Function
///

I hope this helps,

Cor
 
Hi Peter,
Thanks for replying.....

I am storing plain old strings and it seems to work OK...so I should
STILL watch it if the type is a strong reference type ??

No, clone does not make a deep copy.

OTOH, if all you're using is strings, then it doesn't matter. Strings
are immutable. The reason you want a deep copy is so that changes you
make to the objects in arraylist1 don't affect the objects in
arraylist2.

With strings, though, you can't change them anyway. So it really
doesn't matter if you have a shallow or deep copy of the array list.

No, that simply creates another variable pointing to the same
arraylist. You haven't really copied anything.

That's a shallow copy. Both array lists now reference the same objects,
but in two separate lists.
 

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

Back
Top