473,386 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.Clear) -
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 2448
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**********@ftsolutions.comwrote in message
news:%2****************@TK2MSFTNGP02.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.Clear) -
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_material>

"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 "Programming Microsoft Visual Basic
..NET"

</copyrighted_material>

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**********@ftsolutions.comwrote in message
news:%2****************@TK2MSFTNGP02.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.Clear) -
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.GetUpperBound(0)
MessageBox.Show(array1(i).ToString)
Next

' Instantiate a String array named array2.
Dim array2(2) As String
' Copy the elements of array1 to array2.
array1.CopyTo(array2, 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.GetUpperBound(0)
MessageBox.Show("Array1's element " & i.ToString & " contains '"
& _
array1(i).ToString & "' // Array2's element " & i.ToString & "
contains '" & _
array2(i).ToString & "'")
Next

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

<copyrighted_material>

"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 "Programming Microsoft Visual Basic
..NET"

</copyrighted_material>

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**********@ftsolutions.comwrote in message
news:%2****************@TK2MSFTNGP02.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.Clear) -
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_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
Since Mr. Balena says it better than me....

<copyrighted_material>

"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 "Programming Microsoft Visual Basic
.NET"

</copyrighted_material>

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**********@ftsolutions.comwrote in message
news:%2****************@TK2MSFTNGP02.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.Clear) -
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_newsgroups" <ro********@yahoo.comschreef in bericht
news:11**********************@k70g2000cwa.googlegr oups.com...
Since Mr. Balena says it better than me....

<copyrighted_material>

"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 "Programming Microsoft Visual Basic
.NET"

</copyrighted_material>

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**********@ftsolutions.comwrote in message
news:%2****************@TK2MSFTNGP02.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.Clear) -
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
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...
2
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...
15
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...
6
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...
8
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...
4
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,...
5
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...
22
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 =...
0
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...

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.