arraylist copy

S

Sam

Hi,

I have the following code :

Dim rNodeList As New ArrayList
Dim rNodeListNew As New ArrayList

For iCntr As Integer = 0 To tvRelations.Nodes.Count - 1
rNodeList.Add(tvRelations.Nodes(iCntr).Text)
Next

rNodeListNew = rNodeList
rNodeList.RemoveAt(2)

after I've filled rNodeList, it contains 8 elements.
So does rNodeListNew

But when I remove the element 2 of rNodeList, it is also removed in
rNodeListNew. Why ? Do they both point to the same memory location ?
How can I prevent that in vb ?

Thx
 
J

Joseph MCAD

April 8, 2005

Switch the line rNodeListNew = rNodeList to:

rNodeList.CopyTo(rNodeListNew)

This should COPY the objects into the new arraylist. This way the
arraylists do not point to the same objects. If you use the .Clone method
then both arraylists will still point to the same objects. HTH

Joseph MCAD
 
J

Joseph MCAD

April 8, 2005

I am a little confused. Do I have the functions of the Clone and CopyTo
methods mixed up? Thanks!

Joseph MCAD
 
H

Herfried K. Wagner [MVP]

Sam said:
Dim rNodeList As New ArrayList
Dim rNodeListNew As New ArrayList

For iCntr As Integer = 0 To tvRelations.Nodes.Count - 1
rNodeList.Add(tvRelations.Nodes(iCntr).Text)
Next

rNodeListNew = rNodeList
rNodeList.RemoveAt(2)

after I've filled rNodeList, it contains 8 elements.
So does rNodeListNew

But when I remove the element 2 of rNodeList, it is also removed in
rNodeListNew. Why ? Do they both point to the same memory location ?
How can I prevent that in vb ?

Both variables point to the same instance of 'ArrayList'. You can use the
arraylist's 'Clone' method to create a shallow copy of the arraylist:

\\\
Dim al1 As New ArrayList
al1.Add("Bla")
al1.Add("Goo")
Dim al2 As ArrayList = al1.Clone()
al1.Clear()
MsgBox(CStr(al2.Count))
///
 
H

Herfried K. Wagner [MVP]

Joseph MCAD said:
If you use the .Clone method
then both arraylists will still point to the same objects. HTH

That is not true. 'Clone' will create a shallow copy of the arraylist:

\\\
Dim a1 As New ArrayList
Dim a2 As ArrayList = a1.Clone()
MsgBox(CStr(a1 Is a2))
///
 
J

Joseph MCAD

April 8, 2005

Thanks! Then what is the difference between CopyTo and Clone? MSDN
mentioned that clone creates a shallow copy and copyto creates a deep copy?
Thanks again!

Joseph MCAD
 
H

Herfried K. Wagner [MVP]

Joseph,

Joseph MCAD said:
Thanks! Then what is the difference between CopyTo and Clone? MSDN
mentioned that clone creates a shallow copy and copyto creates a deep
copy?

I didn't find any occurance of "deep copy" in the 'ArrayList.CopyTo'
documentation. 'CopyTo' IMO doesn't create a deep copy too. However, there
is a difference between 'ArrayList.Clone' and 'ArrayList.CopyTo': 'CopyTo'
can be used to copy data to an array. Overloaded versions of 'CopyTo' are
provided which allow you to specify what data should be copied.
 
C

Cor Ligthert

"Joseph
Thanks! Then what is the difference between CopyTo and Clone? MSDN
mentioned that clone creates a shallow copy and copyto creates a deep
copy? Thanks again!

A clone makes a copy of the arraylist. While an array is a reference type,
are only the references copied. Any change in a shadow will affect on the
other one as well. Except when it is by instance the removing or adding of a
reference (row), than they grew apart.

A copyto makes a copy of the values in a one dimensional array. Where when
it are objects it is in fact again a shadow of course.

A deep copy makes new copy of all the values and creates a new arraylist
arround those. For that I have never seen a method in Net. You have to do it
yourself. (In my opinion can that be complicated when it is by instance a
jagged array).

I hope this gives some idea's

Cor
 
G

Guest

If I am reading the articles correctly then if an object implements the
Icloneable interfact and it is cloned, then for all the value types the value
is copied to the new object and for all the reference types, only the
reference(pointers) are copied. So what is gained? If I have an arraylist
of value type objects then the .Clone Method will copy the values to a new
arraylist but if the arraylist objects are reference types then only the
reference is copied to the new arraylist. Then when I change a value type in
the new arraylist, the original arraylist is not changed but If I change a
reference type in the new arrraylist, then the original arraylist will also
reflect that change since they both point to the same instance of the object.


Is all this correct?
 
J

J L

Thanks Cor, that does help.

John

"Joseph

A clone makes a copy of the arraylist. While an array is a reference type,
are only the references copied. Any change in a shadow will affect on the
other one as well. Except when it is by instance the removing or adding of a
reference (row), than they grew apart.

A copyto makes a copy of the values in a one dimensional array. Where when
it are objects it is in fact again a shadow of course.

A deep copy makes new copy of all the values and creates a new arraylist
arround those. For that I have never seen a method in Net. You have to do it
yourself. (In my opinion can that be complicated when it is by instance a
jagged array).

I hope this gives some idea's

Cor
 

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