ArrayList deep/shallow copy question

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

    #1

    ArrayList deep/shallow copy question

    Hello,

    Can someone explain me what is the differences between these 2 lines of
    code :

    MyArrayList2 = MyArrayList;
    MyArrayList2 = MyArrayList.Clo ne();

    MyArrayList/MyArrayList2 are aray lists of class objects implementing
    the CompareTo() method, so the list is sortable.

    What happens if i make this call : MyArrayList2.So rt() ?

    Will MyArrayList be sorted too ?


    Regards,
    Cybertof.
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: ArrayList deep/shallow copy question

    Cybertof,

    Here are the differences:

    MyArrayList2 = MyArrayList;

    This will just copy the reference to MyArrayList into MyArrayList2. If
    you add an element to MyArrayList2, then you will see that in MyArrayList,
    because they both point to the same thing.

    MyArrayList2 = MyArrayList.Clo ne();

    This will create a shallow copy of the ArrayList. If the elements are a
    reference, then those references are copied. If they are value types, then
    that is copied to the new array list. Now, if they are all reference types,
    then the two array lists are pointing to the same objects. However, if you
    add a new item to MyArrayList2, then it will not be shown in MyArrayList,
    because MyArrayList2 is a new object, not a reference to the same
    MyArrayList.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m


    "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
    news:MPG.1a1205 8df98acbd19896c 2@msnews.micros oft.com...[color=blue]
    > Hello,
    >
    > Can someone explain me what is the differences between these 2 lines of
    > code :
    >
    > MyArrayList2 = MyArrayList;
    > MyArrayList2 = MyArrayList.Clo ne();
    >
    > MyArrayList/MyArrayList2 are aray lists of class objects implementing
    > the CompareTo() method, so the list is sortable.
    >
    > What happens if i make this call : MyArrayList2.So rt() ?
    >
    > Will MyArrayList be sorted too ?
    >
    >
    > Regards,
    > Cybertof.[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: ArrayList deep/shallow copy question

      Cybertof <cybertof2003no spam@gmx.net> wrote:[color=blue]
      > Can someone explain me what is the differences between these 2 lines of
      > code :
      >
      > MyArrayList2 = MyArrayList;[/color]

      This makes the variable MyArrayList2 have the same value as MyArrayList
      - in other words, they will both be references to the same object.
      [color=blue]
      > MyArrayList2 = MyArrayList.Clo ne();[/color]

      This creates a *new* ArrayList which is a duplicate of the first list.
      [color=blue]
      > MyArrayList/MyArrayList2 are aray lists of class objects implementing
      > the CompareTo() method, so the list is sortable.
      >
      > What happens if i make this call : MyArrayList2.So rt() ?
      >
      > Will MyArrayList be sorted too ?[/color]

      No. They are separate lists (you can remove things from one without it
      affecting the others) - but the references *within* the lists haven't
      been cloned, so if (say) the first element of MyArrayList is a
      reference to a StringBuilder, and you append some text in that
      StringBuilder, that append operation will be visible through the first
      element of MyArrayList2 as they're both references to the same object.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Cybertof

        #4
        Re: ArrayList deep/shallow copy question

        Nicholas & John :

        Thanks for your both answers.


        Regards,
        Cybertof.


        Comment

        Working...