By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,078 Members | 2,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,078 IT Pros & Developers. It's quick & easy.

ArrayList Copy (Deep)

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"Desmond Cassidy" <De*************@T-Online.de> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Nov 21 '05 #2

P: n/a
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(),ArrayL ist)

Cheers,

Desmond.
"Desmond Cassidy" <De*************@T-Online.de> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

Nov 21 '05 #3

P: n/a
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 Cassidy" <De*************@T-Online.de> wrote in message
news:OF**************@TK2MSFTNGP14.phx.gbl...
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(),ArrayL ist)

Cheers,

Desmond.
"Desmond Cassidy" <De*************@T-Online.de> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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


Nov 21 '05 #4

P: n/a
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.BinaryForm atter
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.BinaryForm atter
Dim mem As New
IO.MemoryStream(Convert.FromBase64String(arraystri ng))
Return DirectCast(bf.Deserialize(mem), ArrayList)
End Function
///

I hope this helps,

Cor

Nov 21 '05 #5

P: n/a
Many thanks Cor.....


"Cor Ligthert" <no************@planet.nl> wrote in message
news:up**************@TK2MSFTNGP10.phx.gbl...
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.BinaryForm atter
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.BinaryForm atter
Dim mem As New
IO.MemoryStream(Convert.FromBase64String(arraystri ng))
Return DirectCast(bf.Deserialize(mem), ArrayList)
End Function
///

I hope this helps,

Cor

Nov 21 '05 #6

P: n/a
On 2005-04-24, Desmond Cassidy <De*************@T-Online.de> wrote:
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.
"Desmond Cassidy" <De*************@T-Online.de> wrote in message
news:OF**************@TK2MSFTNGP14.phx.gbl...
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
No, that simply creates another variable pointing to the same
arraylist. You haven't really copied anything.
A Deep Copy is
larrlstArrayListSave =
DirectCast(larrlstArrayListOriginal.Clone(),ArrayL ist)


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

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.