Make copy of arraylist

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tshad

    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


  • tshad

    #2
    Re: Make copy of arraylist

    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" <tscheiderich@f tsolutions.comw rote in message
    news:%23VxPqbPB HHA.3836@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
    >

    Comment

    • rowe_newsgroups

      #3
      Re: Make copy of arraylist

      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" <tscheiderich@f tsolutions.comw rote in message
      news:%23VxPqbPB HHA.3836@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

      Comment

      • MikeMc

        #4
        Re: Make copy of arraylist

        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" <tscheiderich@f tsolutions.comw rote in message
        news:%23VxPqbPB HHA.3836@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
        >
        >
        >

        Comment

        • RobinS

          #5
          Re: Make copy of arraylist

          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" <rowe_email@yah oo.comwrote in message
          news:1163187288 .832134.275970@ 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" <tscheiderich@f tsolutions.comw rote in message
          >news:%23VxPqbP BHHA.3836@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
          >
          >

          Comment

          • Cor Ligthert [MVP]

            #6
            Re: Make copy of arraylist

            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.



            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.



            I hope this helps,

            Cor

            "rowe_newsgroup s" <rowe_email@yah oo.comschreef in bericht
            news:1163187288 .832134.275970@ 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" <tscheiderich@f tsolutions.comw rote in message
            >news:%23VxPqbP BHHA.3836@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
            >
            >

            Comment

            Working...