ArrayList Copy (Deep)

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

    ArrayList Copy (Deep)

    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 saved Arraylist.

    Thus the code I would like would be similar to the following

    larrlstArrayLis tSave = larrlstArrayLis tOriginal
    larrlstArrayLis tOriginal.Clear
    BUT the larrlstArrayLis tSave should still have the collection in there.

    I looked at .Clone and .CopyTo but they don't seem to cut !!!

    It's annoying the hell out of me !!!

    Any ideas ???

    Cheers,

    Desmond


  • Peter van der Goes

    #2
    Re: ArrayList Copy (Deep)


    "Desmond Cassidy" <Desmond.Cassid y@T-Online.de> wrote in message
    news:%23qjxXDNS FHA.2604@TK2MSF TNGP10.phx.gbl. ..[color=blue]
    > 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 saved Arraylist.
    >
    > Thus the code I would like would be similar to the following
    >
    > larrlstArrayLis tSave = larrlstArrayLis tOriginal
    > larrlstArrayLis tOriginal.Clear
    > BUT the larrlstArrayLis tSave should still have the collection in
    > there.
    >
    > I looked at .Clone and .CopyTo but they don't seem to cut !!!
    >
    > It's annoying the hell out of me !!!
    >
    > Any ideas ???
    >
    > Cheers,
    >
    > Desmond[/color]
    Clone will do it if the elements in your ArrayList are primitives, but if
    you're storing reference types in the ArrayList, it looks like you'll have
    to do the necessary work of cloning each of the actual objects in your own
    "DeepCopy" method.
    I'm a bit confused by your example, however, as Clear() simply sets the
    ArrayList Count property to zero, thus the references established in the
    second ArrayList still point to the objects not deleted by Clear().
    There may be a better solution/technique that I've missed.

    --
    Peter [MVP Visual Developer]
    Jack of all trades, master of none.


    Comment

    • Desmond Cassidy

      #3
      Re: ArrayList Copy (Deep)

      OK...I found something written about this a few weeks ago...and it seems to
      have caused some confusion.

      CopyTo and Clone seem to be the buzz words.

      In my case I 'should' use Clone as this creates a DEEP copy whereas
      CopyTo will NOT work with an Arraylist.
      CopyTo will copy an Arraylist to an Array Type and NOT an Arraylist.

      Thus, and I would like the gurus in you to confirm this.....

      A Shallow copy is
      larrlstArrayLis tSave = larrlstArrayLis tOriginal
      A Deep Copy is
      larrlstArrayLis tSave =
      DirectCast(larr lstArrayListOri ginal.Clone(),A rrayList)

      Cheers,

      Desmond.


      "Desmond Cassidy" <Desmond.Cassid y@T-Online.de> wrote in message
      news:%23qjxXDNS FHA.2604@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > 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 saved Arraylist.
      >
      > Thus the code I would like would be similar to the following
      >
      > larrlstArrayLis tSave = larrlstArrayLis tOriginal
      > larrlstArrayLis tOriginal.Clear
      > BUT the larrlstArrayLis tSave should still have the collection in
      > there.
      >
      > I looked at .Clone and .CopyTo but they don't seem to cut !!!
      >
      > It's annoying the hell out of me !!!
      >
      > Any ideas ???
      >
      > Cheers,
      >
      > Desmond
      >[/color]


      Comment

      • Desmond Cassidy

        #4
        Re: ArrayList Copy (Deep)

        Hi Peter,
        Thanks for replying.....

        I am storing plain old strings and it seems to work OK...so I should
        STILL watch it if the type is a strong reference type ??

        Cheers,

        Desmond.

        "Desmond Cassidy" <Desmond.Cassid y@T-Online.de> wrote in message
        news:OFErjnNSFH A.3420@TK2MSFTN GP14.phx.gbl...[color=blue]
        > OK...I found something written about this a few weeks ago...and it seems
        > to have caused some confusion.
        >
        > CopyTo and Clone seem to be the buzz words.
        >
        > In my case I 'should' use Clone as this creates a DEEP copy whereas
        > CopyTo will NOT work with an Arraylist.
        > CopyTo will copy an Arraylist to an Array Type and NOT an Arraylist.
        >
        > Thus, and I would like the gurus in you to confirm this.....
        >
        > A Shallow copy is
        > larrlstArrayLis tSave = larrlstArrayLis tOriginal
        > A Deep Copy is
        > larrlstArrayLis tSave =
        > DirectCast(larr lstArrayListOri ginal.Clone(),A rrayList)
        >
        > Cheers,
        >
        > Desmond.
        >
        >
        > "Desmond Cassidy" <Desmond.Cassid y@T-Online.de> wrote in message
        > news:%23qjxXDNS FHA.2604@TK2MSF TNGP10.phx.gbl. ..[color=green]
        >> 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 saved Arraylist.
        >>
        >> Thus the code I would like would be similar to the following
        >>
        >> larrlstArrayLis tSave = larrlstArrayLis tOriginal
        >> larrlstArrayLis tOriginal.Clear
        >> BUT the larrlstArrayLis tSave should still have the collection in
        >> there.
        >>
        >> I looked at .Clone and .CopyTo but they don't seem to cut !!!
        >>
        >> It's annoying the hell out of me !!!
        >>
        >> Any ideas ???
        >>
        >> Cheers,
        >>
        >> Desmond
        >>[/color]
        >
        >[/color]


        Comment

        • Cor Ligthert

          #5
          Re: ArrayList Copy (Deep)

          Desmond.

          You have to make a deepCopy as Peter already wrote. I never found a standard
          answer how to make a deepCopy on MSDN.

          Problem is that it is probably impossible because of the fact that an
          arraylist holds references to objects, wich can be everything. In fact they
          can all be different, not very usefull, however it is possible.

          Do you have any idea how to copy the content of this arraylist without
          knowing what it is. I don't for me it looks impossible (I as well don't know
          how to use it when I don't keep track what every reference per index holds).
          \\\
          Dim ar As New ArrayList
          ar.Add(New Integer)
          ar.Add(New Double)
          Dim str As String
          ar.Add(str)
          Dim arl2 As New ArrayList
          Dim arl3 As New ArrayList
          arl2.Add(arl3)
          ar.Add(arl2)
          ///
          Before you say this is crazy, I try only to show you what it can be.

          However serializing and deserializing can be a good workaround for this
          problem.

          \\\
          Private Function SerializeArrayl ist(ByVal _
          arraylst As ArrayList) As String
          Dim bf As New
          Runtime.Seriali zation.Formatte rs.Binary.Binar yFormatter
          Dim mem As New IO.MemoryStream
          bf.Serialize(me m, arraylst)
          Return Convert.ToBase6 4String(mem.ToA rray())
          End Function
          Private Function DeserializeArra ylist(ByVal _
          arraystring As String) As ArrayList
          Dim bf As New
          Runtime.Seriali zation.Formatte rs.Binary.Binar yFormatter
          Dim mem As New
          IO.MemoryStream (Convert.FromBa se64String(arra ystring))
          Return DirectCast(bf.D eserialize(mem) , ArrayList)
          End Function
          ///

          I hope this helps,

          Cor



          Comment

          • Desmond Cassidy

            #6
            Re: ArrayList Copy (Deep)

            Many thanks Cor.....




            "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
            news:updNbKQSFH A.1396@TK2MSFTN GP10.phx.gbl...[color=blue]
            > Desmond.
            >
            > You have to make a deepCopy as Peter already wrote. I never found a
            > standard answer how to make a deepCopy on MSDN.
            >
            > Problem is that it is probably impossible because of the fact that an
            > arraylist holds references to objects, wich can be everything. In fact
            > they can all be different, not very usefull, however it is possible.
            >
            > Do you have any idea how to copy the content of this arraylist without
            > knowing what it is. I don't for me it looks impossible (I as well don't
            > know how to use it when I don't keep track what every reference per index
            > holds).
            > \\\
            > Dim ar As New ArrayList
            > ar.Add(New Integer)
            > ar.Add(New Double)
            > Dim str As String
            > ar.Add(str)
            > Dim arl2 As New ArrayList
            > Dim arl3 As New ArrayList
            > arl2.Add(arl3)
            > ar.Add(arl2)
            > ///
            > Before you say this is crazy, I try only to show you what it can be.
            >
            > However serializing and deserializing can be a good workaround for this
            > problem.
            >
            > \\\
            > Private Function SerializeArrayl ist(ByVal _
            > arraylst As ArrayList) As String
            > Dim bf As New
            > Runtime.Seriali zation.Formatte rs.Binary.Binar yFormatter
            > Dim mem As New IO.MemoryStream
            > bf.Serialize(me m, arraylst)
            > Return Convert.ToBase6 4String(mem.ToA rray())
            > End Function
            > Private Function DeserializeArra ylist(ByVal _
            > arraystring As String) As ArrayList
            > Dim bf As New
            > Runtime.Seriali zation.Formatte rs.Binary.Binar yFormatter
            > Dim mem As New
            > IO.MemoryStream (Convert.FromBa se64String(arra ystring))
            > Return DirectCast(bf.D eserialize(mem) , ArrayList)
            > End Function
            > ///
            >
            > I hope this helps,
            >
            > Cor
            >
            >
            >[/color]


            Comment

            • David

              #7
              Re: ArrayList Copy (Deep)

              On 2005-04-24, Desmond Cassidy <Desmond.Cassid y@T-Online.de> wrote:[color=blue]
              > Hi Peter,
              > Thanks for replying.....
              >
              > I am storing plain old strings and it seems to work OK...so I should
              > STILL watch it if the type is a strong reference type ??[/color]

              No, clone does not make a deep copy.

              OTOH, if all you're using is strings, then it doesn't matter. Strings
              are immutable. The reason you want a deep copy is so that changes you
              make to the objects in arraylist1 don't affect the objects in
              arraylist2.

              With strings, though, you can't change them anyway. So it really
              doesn't matter if you have a shallow or deep copy of the array list.
              [color=blue]
              > "Desmond Cassidy" <Desmond.Cassid y@T-Online.de> wrote in message
              > news:OFErjnNSFH A.3420@TK2MSFTN GP14.phx.gbl...[color=green]
              >> OK...I found something written about this a few weeks ago...and it seems
              >> to have caused some confusion.
              >>
              >> CopyTo and Clone seem to be the buzz words.
              >>
              >> In my case I 'should' use Clone as this creates a DEEP copy whereas
              >> CopyTo will NOT work with an Arraylist.
              >> CopyTo will copy an Arraylist to an Array Type and NOT an Arraylist.
              >>
              >> Thus, and I would like the gurus in you to confirm this.....
              >>
              >> A Shallow copy is
              >> larrlstArrayLis tSave = larrlstArrayLis tOriginal[/color][/color]

              No, that simply creates another variable pointing to the same
              arraylist. You haven't really copied anything.
              [color=blue][color=green]
              >> A Deep Copy is
              >> larrlstArrayLis tSave =
              >> DirectCast(larr lstArrayListOri ginal.Clone(),A rrayList)[/color][/color]

              That's a shallow copy. Both array lists now reference the same objects,
              but in two separate lists.

              Comment

              Working...