Home All Groups Group Topic Archive Search About

Make copy of arraylist

Author
10 Nov 2006 6:34 PM
tshad
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

Author
10 Nov 2006 6:53 PM
tshad
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

Show quoteHide quote
"tshad" <tscheider***@ftsolutions.com> wrote in message
news:%23VxPqbPBHHA.3836@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
>
Author
10 Nov 2006 7:34 PM
rowe_newsgroups
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:
Show quoteHide quote
> 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" <tscheider***@ftsolutions.com> wrote in message
> news:%23VxPqbPBHHA.3836@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
> >
Author
10 Nov 2006 8:15 PM
MikeMc
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

Show quoteHide quote
"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" <tscheider***@ftsolutions.com> wrote in message
> > news:%23VxPqbPBHHA.3836@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
> > >
>
>
Author
10 Nov 2006 10:49 PM
RobinS
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.

Show quoteHide quote
"rowe_newsgroups" <rowe_em***@yahoo.com> wrote in message
news:1163187288.832134.275970@k70g2000cwa.googlegroups.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" <tscheider***@ftsolutions.com> wrote in message
>> news:%23VxPqbPBHHA.3836@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
>> >
>
Author
11 Nov 2006 5:16 AM
Cor Ligthert [MVP]
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/library/system.collections.arraylist.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?ID=7ffd296f-9e81-47e6-88dc-61641f5c8d9d

I hope this helps,

Cor

Show quoteHide quote
"rowe_newsgroups" <rowe_em***@yahoo.com> schreef in bericht
news:1163187288.832134.275970@k70g2000cwa.googlegroups.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" <tscheider***@ftsolutions.com> wrote in message
>> news:%23VxPqbPBHHA.3836@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
>> >
>