473,795 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Make copy of arraylist

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.Cle ar) -
arrayList1 is also cleared.

I want to create a copy of the arrayList, which I can do looping through the
arrayList1 and adding to arrayList2, but is there an easier way to do this?

Thanks,

Tom
Nov 10 '06 #1
5 2472
Apparently, there are 2 methods: Clone and CopyTo.

What is the difference between the 2?

Clone is supposed to do a "shallow copy"? What is that and why is it
different than CopyTo?

Thanks,

Tom

"tshad" <ts**********@f tsolutions.comw rote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
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.Cle ar) -
arrayList1 is also cleared.

I want to create a copy of the arrayList, which I can do looping through
the arrayList1 and adding to arrayList2, but is there an easier way to do
this?

Thanks,

Tom

Nov 10 '06 #2
Since Mr. Balena says it better than me....

<copyrighted_ma terial>

"The Clone method can create either a shallow copy or a deep copy of
the object. A shallow copy creates only a copy of the object in
question; it doesn't make copies of secondary objects referenced by
it." - From Francesco Balena's book "Programmin g Microsoft Visual Basic
..NET"

</copyrighted_mat erial>

Thanks,

Seth Rowe
tshad wrote:
Apparently, there are 2 methods: Clone and CopyTo.

What is the difference between the 2?

Clone is supposed to do a "shallow copy"? What is that and why is it
different than CopyTo?

Thanks,

Tom

"tshad" <ts**********@f tsolutions.comw rote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
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.Cle ar) -
arrayList1 is also cleared.

I want to create a copy of the arrayList, which I can do looping through
the arrayList1 and adding to arrayList2, but is there an easier way to do
this?

Thanks,

Tom
Nov 10 '06 #3
CopyTo inserts a copy of the elements in an array into a second array. Once
copied the elements in both arrays are separte but equal.

Heres some code that demonstrates CopyTo in acton:

' Instantiate and intialize the values of a String array named array1.
Dim array1(2) As String
array1 = {"bread", "soup", "beans"}

' Show the values of array1.
For i As Integer = 0 To array1.GetUpper Bound(0)
MessageBox.Show (array1(i).ToSt ring)
Next

' Instantiate a String array named array2.
Dim array2(2) As String
' Copy the elements of array1 to array2.
array1.CopyTo(a rray2, 0)

' Change the first element of array1.
array1(0) = "Was bread, now desert"

' Display the values of array1 and array 2 to show
' they are two separate arrays.
For i As Integer = 0 To array2.GetUpper Bound(0)
MessageBox.Show ("Array1's element " & i.ToString & " contains '"
& _
array1(i).ToStr ing & "' // Array2's element " & i.ToString & "
contains '" & _
array2(i).ToStr ing & "'")
Next

--
Mike McIntyre http://www.getdotnetcode.com
"rowe_newsgroup s" wrote:
Since Mr. Balena says it better than me....

<copyrighted_ma terial>

"The Clone method can create either a shallow copy or a deep copy of
the object. A shallow copy creates only a copy of the object in
question; it doesn't make copies of secondary objects referenced by
it." - From Francesco Balena's book "Programmin g Microsoft Visual Basic
..NET"

</copyrighted_mat erial>

Thanks,

Seth Rowe
tshad wrote:
Apparently, there are 2 methods: Clone and CopyTo.

What is the difference between the 2?

Clone is supposed to do a "shallow copy"? What is that and why is it
different than CopyTo?

Thanks,

Tom

"tshad" <ts**********@f tsolutions.comw rote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
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.Cle ar) -
arrayList1 is also cleared.
>
I want to create a copy of the arrayList, which I can do looping through
the arrayList1 and adding to arrayList2, but is there an easier way to do
this?
>
Thanks,
>
Tom
>

Nov 10 '06 #4
Seth,
Isn't that a great book? I read it cover to cover.
Have you seen his other one, on Standard Practices?
It has a lot of neat tricks and helpful information
in it, too.
Robin S.

"rowe_newsgroup s" <ro********@yah oo.comwrote in message
news:11******** **************@ k70g2000cwa.goo glegroups.com.. .
Since Mr. Balena says it better than me....

<copyrighted_ma terial>

"The Clone method can create either a shallow copy or a deep copy of
the object. A shallow copy creates only a copy of the object in
question; it doesn't make copies of secondary objects referenced by
it." - From Francesco Balena's book "Programmin g Microsoft Visual Basic
.NET"

</copyrighted_mat erial>

Thanks,

Seth Rowe
tshad wrote:
>Apparently, there are 2 methods: Clone and CopyTo.

What is the difference between the 2?

Clone is supposed to do a "shallow copy"? What is that and why is it
different than CopyTo?

Thanks,

Tom

"tshad" <ts**********@f tsolutions.comw rote in message
news:%2******* *********@TK2MS FTNGP02.phx.gbl ...
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.Cle ar) -
arrayList1 is also cleared.

I want to create a copy of the arrayList, which I can do looping
through
the arrayList1 and adding to arrayList2, but is there an easier way to
do
this?

Thanks,

Tom

Nov 10 '06 #5
As mr. Balena wrote it as you told, than he made in my idea a mistake. The
clone method can only make a shallow copy.

http://msdn2.microsoft.com/en-us/lib...ist.clone.aspx

To do a deep copy (complete) in the case of the arraylist without needing an
fixed lenght array than you need to serialize and deserialize it.

http://www.vb-tips.com/dbPages.aspx?...c-61641f5c8d9d

I hope this helps,

Cor

"rowe_newsgroup s" <ro********@yah oo.comschreef in bericht
news:11******** **************@ k70g2000cwa.goo glegroups.com.. .
Since Mr. Balena says it better than me....

<copyrighted_ma terial>

"The Clone method can create either a shallow copy or a deep copy of
the object. A shallow copy creates only a copy of the object in
question; it doesn't make copies of secondary objects referenced by
it." - From Francesco Balena's book "Programmin g Microsoft Visual Basic
.NET"

</copyrighted_mat erial>

Thanks,

Seth Rowe
tshad wrote:
>Apparently, there are 2 methods: Clone and CopyTo.

What is the difference between the 2?

Clone is supposed to do a "shallow copy"? What is that and why is it
different than CopyTo?

Thanks,

Tom

"tshad" <ts**********@f tsolutions.comw rote in message
news:%2******* *********@TK2MS FTNGP02.phx.gbl ...
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.Cle ar) -
arrayList1 is also cleared.

I want to create a copy of the arrayList, which I can do looping
through
the arrayList1 and adding to arrayList2, but is there an easier way to
do
this?

Thanks,

Tom

Nov 11 '06 #6

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

Similar topics

0
1252
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 ?
2
2677
by: Martin Ortiz | last post by:
Ugh.... All classes are copy by reference, even if you use "ByVal" and NOT "ByRef" it's still a copy by reference. Of course, as a consequence, if you change any values of the object you passed in, the original object is affected. I expect this with arrays, but not with single objects being passed "ByVal" to functions.... Is there a sane to get "ByVal" passing of parameters? (copying fields is not
15
8339
by: Sam | last post by:
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
6
4324
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
3139
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
4
1442
by: OpticTygre | last post by:
If I pass an object to another form via the new form's tag property, I want to create an object exactly like it, with it's properties and all, but have it be a copy of the object passed through, and not just a reference to the object. Example: Public Sub Form_Load(ByVal sender as Object, ByVal e as EventArgs) Dim obj as Object = Activator.CreateInstance(Me.Tag.GetType())
5
14163
by: res7cxbi | last post by:
I like to implement and ArrayList like Java's and C# in C++ but I have no clue how to start I really don't want to use Managed C++ and use ArrayList that way - I want to keep it "unmanaged" Can anyone point me to a resource that can tell me how to do this? Thanks in advance
22
15949
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();
0
1344
by: fmlamela | last post by:
Hi, I have to create a bi-dimensional matrix, where each row is a int array of a specific size. For adding a new row, I use the ArrayList.Add (int ) command. I would like now to convert this ArrayList object, to a int variable. I only managed to do it with a double "for loop" for reading each row and column. Considering we have N rows and M columns, I have tried first trying to copy each single row: int arr = new int;...
0
9673
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
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
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
10165
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
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9044
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...
0
6783
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();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.