473,804 Members | 3,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

arraylist copy

Sam
Hi,

I have the following code :

Dim rNodeList As New ArrayList
Dim rNodeListNew As New ArrayList

For iCntr As Integer = 0 To tvRelations.Nod es.Count - 1
rNodeList.Add(t vRelations.Node s(iCntr).Text)
Next

rNodeListNew = rNodeList
rNodeList.Remov eAt(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

Nov 21 '05
15 8342
"J L" <jo**@marymonte .com> schrieb:
What does "shallow" and "deep" copy mean?


An insight into cloning objects in .NET
<URL:http://www.codeproject .com/dotnet/Clone.asp>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #11

"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
Nov 21 '05 #12
JL

See my message to Joseph

Cor
Nov 21 '05 #13
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(point ers) 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?

"Herfried K. Wagner [MVP]" wrote:
"J L" <jo**@marymonte .com> schrieb:
What does "shallow" and "deep" copy mean?


An insight into cloning objects in .NET
<URL:http://www.codeproject .com/dotnet/Clone.asp>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #14
J L
Thanks Cor, that does help.

John

On Sat, 9 Apr 2005 09:21:15 +0200, "Cor Ligthert"
<no************ @planet.nl> wrote:

"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


Nov 21 '05 #15
April 9, 2005

Thanks for everybody's replies!

Joseph MCAD
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:uc******** ******@tk2msftn gp13.phx.gbl...

"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

Nov 21 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1896
by: george r smith | last post by:
I am trying to create an arrayList that contains multiple arrayLists. My code attempt is below. The question I have is how can I get away from creating another pAttribute list than can be added to pItem. As I understand it (and I coded it to test) if I try to use pAttribute again by clearing with pAttribute.Clear because it is a shallow copy the pItem is also cleared. I guess I need a "deep copy" but can't find any documentation on it. ...
1
3396
by: Marc Lefebvre | last post by:
there is an easy way to make a deep copy of an arraylist ? suppose I have an arraylist of COptions implements ICloneable I have need to overload the Clone() methode of a derived ArrayList (OptionsArrayList) And Overload the Clone() methode of my COptions There is an otherway to do deep copy ?
7
14292
by: William Stacey [MVP] | last post by:
I can think of a couple ways to do this, but was wonder what the fastest way you can think of. Want to take any arraylist of > 1 element and make the first element the last, and all the rest of the elements will move up one position. This will be called a lot, so looking for speed. This just saving the index 0 as tmp and then enum the list to move up the elements, then put the tmp object in last pos is the best way? Cheers --...
3
9696
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList atemp = new ArrayList(); // THE ARRAY StreamWriter sw = new StreamWriter(@"C:\temp\fred.html");
7
2092
by: Adam | last post by:
Hi all, In my VB.NET code below, I try to change the user name in my arraylist from Ted to Bob, but instead it adds a new user to the arraylist named Bob. Can anyone explain why this happens and how I might modify a structure in an arraylist without having to completely remove it and re-add the structure to the arraylist? I just want to iterate throught the arraylist and change items based on certain conditions. Thanks in advance!
6
4328
by: Desmond Cassidy | last post by:
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...
8
3141
by: Esmail Bonakdarian | last post by:
Hello, I am relatively new to C# but like it. I have run into a problem to which I haven't been able to find a solution despite looking. I have an ArrayList that contains several RichTextBoxes (via Add()) I am trying to create a second ArrayList with this information, but I want to make a DEEP copy instead of a shallow one, so that
5
2475
by: tshad | last post by:
How do you easily make a copy of an arraylist? If you do: arrayList2 = arrayList1 You get a pointer so that if you clear arrayList2 (arrayList2.Clear) - arrayList1 is also cleared. I want to create a copy of the arrayList, which I can do looping through the
22
15952
by: Steven Blair | last post by:
I need to perform a Deep Copy on an ArrayList. I wrote a small sample app to prove this could be done: ArrayList a = new ArrayList(); ArrayList b = new ArrayList(); a.Add("Hello"); b = (ArrayList) a.Clone();
3
2632
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: code:------------------------------------------------------------------------------ class Program { static public ArrayList MyArrayList = new ArrayList();
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10580
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9157
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7621
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3821
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.