Does the clone() method of ArrayList<> make a copy of the objects in the ArrayList?

Discussion in 'Java' started by xz, Aug 3, 2007.

  1. xz

    xz Guest

    anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
    anotherArrayList.get(0).makeSomeChange();

    Will the change made in the second line make effect on
    oneArrayList.get(0)?
     
    xz, Aug 3, 2007
    #1
    1. Advertising

  2. Re: Does the clone() method of ArrayList<> make a copy of the objectsin the ArrayList?

    xz wrote:
    > anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
    > anotherArrayList.get(0).makeSomeChange();
    >
    > Will the change made in the second line make effect on
    > oneArrayList.get(0)?


    clone is a shallow clone not a deep clone so yes.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Aug 3, 2007
    #2
    1. Advertising

  3. Re: Does the clone() method of ArrayList<> make a copy of the objectsin the ArrayList?

    Arne Vajhøj wrote:
    > xz wrote:
    >> anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
    >> anotherArrayList.get(0).makeSomeChange();
    >>
    >> Will the change made in the second line make effect on
    >> oneArrayList.get(0)?

    >
    > clone is a shallow clone not a deep clone so yes.
    >
    > Arne


    Arne:

    I know that it says in the docs that ArrayList.clone() is a shallow copy
    and that the elements themselves are not copied. Why then does the
    following code produce the following results (or have I gone completely
    around the bend today?).

    import java.util.*;

    public class test9 {
    public static void main(String[] args) {
    ArrayList<Integer> list1 = new ArrayList<Integer>();
    list1.add(10);
    list1.add(11);
    list1.add(12);
    ArrayList list2 = (ArrayList)list1.clone();
    list1.set(0,20);
    list1.add(30);
    for (int i=0; i<list1.size(); i++)
    System.out.print(list1.get(i)+" ");
    System.out.println();
    for (int i=0; i<list2.size(); i++)
    System.out.print(list2.get(i)+" ");
    }
    }

    C:\Documents and Settings\Knute Johnson>java test9
    20 11 12 30
    10 11 12

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Aug 4, 2007
    #3
  4. xz

    Twisted Guest

    On Aug 3, 8:28 pm, Knute Johnson <>
    wrote:
    > Arne Vajhøj wrote:
    > > xz wrote:
    > >> anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
    > >> anotherArrayList.get(0).makeSomeChange();

    >
    > >> Will the change made in the second line make effect on
    > >> oneArrayList.get(0)?

    >
    > > clone is a shallow clone not a deep clone so yes.

    >
    > > Arne

    >
    > Arne:
    >
    > I know that it says in the docs that ArrayList.clone() is a shallow copy
    > and that the elements themselves are not copied. Why then does the
    > following code produce the following results (or have I gone completely
    > around the bend today?).
    >
    > import java.util.*;
    >
    > public class test9 {
    > public static void main(String[] args) {
    > ArrayList<Integer> list1 = new ArrayList<Integer>();
    > list1.add(10);
    > list1.add(11);
    > list1.add(12);
    > ArrayList list2 = (ArrayList)list1.clone();
    > list1.set(0,20);
    > list1.add(30);


    These modify the cloned list, not the Integer objects. Try replacing
    the Integers with StringBuffers, and change the set line to actually
    edit the string buffer, i.e. list1.get(0).append("foobar");

    Then your first list output should show the first item with "foobar"
    on the end and your fourth added item; your second list output should
    still omit the fourth item but also have the first item have "foobar"
    on the end.
     
    Twisted, Aug 4, 2007
    #4
  5. Re: Does the clone() method of ArrayList<> make a copy of the objectsin the ArrayList?

    Knute Johnson wrote:
    > Arne Vajhøj wrote:
    >> xz wrote:
    >>> anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
    >>> anotherArrayList.get(0).makeSomeChange();
    >>>
    >>> Will the change made in the second line make effect on
    >>> oneArrayList.get(0)?

    >>
    >> clone is a shallow clone not a deep clone so yes.
    >>
    >> Arne

    >
    > Arne:
    >
    > I know that it says in the docs that ArrayList.clone() is a shallow copy
    > and that the elements themselves are not copied. Why then does the
    > following code produce the following results (or have I gone completely
    > around the bend today?).
    >
    > import java.util.*;
    >
    > public class test9 {
    > public static void main(String[] args) {
    > ArrayList<Integer> list1 = new ArrayList<Integer>();
    > list1.add(10);
    > list1.add(11);
    > list1.add(12);
    > ArrayList list2 = (ArrayList)list1.clone();
    > list1.set(0,20);
    > list1.add(30);
    > for (int i=0; i<list1.size(); i++)
    > System.out.print(list1.get(i)+" ");
    > System.out.println();
    > for (int i=0; i<list2.size(); i++)
    > System.out.print(list2.get(i)+" ");
    > }
    > }
    >
    > C:\Documents and Settings\Knute Johnson>java test9
    > 20 11 12 30
    > 10 11 12
    >


    The difference between a deep and shallow clone is difficult to see if
    you only add immutable objects, such as Integer:

    import java.util.*;

    public class ListCloneTest {
    public static void main(String[] args) {
    ArrayList<StringBuilder> list1 = new ArrayList<StringBuilder>();
    list1.add(new StringBuilder("aaa "));
    ArrayList list2 = (ArrayList) list1.clone();
    StringBuilder builder = list1.get(0);
    builder
    .append("Both lists point to the same StringBuilder");
    for (int i = 0; i < list1.size(); i++)
    System.out.print(list1.get(i) + " ");
    System.out.println();
    for (int i = 0; i < list2.size(); i++)
    System.out.print(list2.get(i) + " ");
    }
    }

    aaa Both lists point to the same StringBuilder
    aaa Both lists point to the same StringBuilder

    Patricia
     
    Patricia Shanahan, Aug 4, 2007
    #5
  6. xz

    xz Guest

    On Aug 3, 7:28 pm, Knute Johnson <>
    wrote:
    > Arne Vajhøj wrote:
    > > xz wrote:
    > >> anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
    > >> anotherArrayList.get(0).makeSomeChange();

    >
    > >> Will the change made in the second line make effect on
    > >> oneArrayList.get(0)?

    >
    > > clone is a shallow clone not a deep clone so yes.

    >
    > > Arne

    >
    > Arne:
    >
    > I know that it says in the docs that ArrayList.clone() is a shallow copy
    > and that the elements themselves are not copied. Why then does the
    > following code produce the following results (or have I gone completely
    > around the bend today?).
    >
    > import java.util.*;
    >
    > public class test9 {
    > public static void main(String[] args) {
    > ArrayList<Integer> list1 = new ArrayList<Integer>();
    > list1.add(10);
    > list1.add(11);
    > list1.add(12);
    > ArrayList list2 = (ArrayList)list1.clone();

    this line copies every item in list1 into list2 such that
    list1.get(i) = list2.get(i) for i = 0, 1, 2,
    which means, two references (0th item in list1 and 0th item in list2)
    point to the same thing, but they themselves are not identical.

    > list1.set(0,20);

    now you changed the 0th item in list1, in other words, you let the the
    0th item in list1, which is a reference, point to another thing (20).
    This process has nothing to do with list2, meaning 0th item in list2
    still points to 10.

    > list1.add(30);
    > for (int i=0; i<list1.size(); i++)
    > System.out.print(list1.get(i)+" ");
    > System.out.println();
    > for (int i=0; i<list2.size(); i++)
    > System.out.print(list2.get(i)+" ");
    > }
    >
    > }
    >
    > C:\Documents and Settings\Knute Johnson>java test9
    > 20 11 12 30
    > 10 11 12
    >


    However, as I konw ArrayList can only deal with classes, e.g. Integer,
    but not primitive types like int. How come your code compiles?
     
    xz, Aug 4, 2007
    #6
  7. Re: Does the clone() method of ArrayList<> make a copy of the objectsin the ArrayList?

    xz wrote:
    >> ArrayList<Integer> list1 = new ArrayList<Integer>();
    >> list1.add(10);
    >> list1.add(11);
    >> list1.add(12);


    > However, as I konw ArrayList can only deal with classes, e.g. Integer,
    > but not primitive types like int. How come your code compiles?


    Newer Java versions (1.5+) can auto convert between int and Integer.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Aug 4, 2007
    #7
  8. Re: Does the clone() method of ArrayList<> make a copy of the objectsin the ArrayList?

    Knute Johnson wrote:
    > Arne Vajhøj wrote:
    >> clone is a shallow clone not a deep clone so yes.


    > I know that it says in the docs that ArrayList.clone() is a shallow copy
    > and that the elements themselves are not copied. Why then does the
    > following code produce the following results (or have I gone completely
    > around the bend today?).


    As other has already stated, then you are changing the references
    in the lists not the objects pointed to by the references.

    The docs are correct.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Aug 4, 2007
    #8
  9. xz

    xz Guest

    Do you mean now wherever you can use Integer, you can use int, and
    vice versa?

    That's cool. Didn't know that before.

    On Aug 3, 10:11 pm, Arne Vajhøj <> wrote:
    > xz wrote:
    > >> ArrayList<Integer> list1 = new ArrayList<Integer>();
    > >> list1.add(10);
    > >> list1.add(11);
    > >> list1.add(12);

    > > However, as I konw ArrayList can only deal with classes, e.g. Integer,
    > > but not primitive types like int. How come your code compiles?

    >
    > Newer Java versions (1.5+) can auto convert between int and Integer.
    >
    > Arne
     
    xz, Aug 4, 2007
    #9
  10. Re: Does the clone() method of ArrayList<> make a copy of the objectsin the ArrayList?

    Arne Vajhøj wrote:
    > Knute Johnson wrote:
    >> Arne Vajhøj wrote:
    >>> clone is a shallow clone not a deep clone so yes.

    >
    >> I know that it says in the docs that ArrayList.clone() is a shallow
    >> copy and that the elements themselves are not copied. Why then does
    >> the following code produce the following results (or have I gone
    >> completely around the bend today?).

    >
    > As other has already stated, then you are changing the references
    > in the lists not the objects pointed to by the references.
    >
    > The docs are correct.
    >
    > Arne


    Thanks very much everybody. I was confused when I wrote the test code
    because it did copy the references. That was not as I expected. Using
    a mutable object it is immediately obvious though.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Aug 4, 2007
    #10
  11. Re: Does the clone() method of ArrayList<> make a copy of the objectsin the ArrayList?

    xz wrote:
    > Do you mean now wherever you can use Integer, you can use int, and
    > vice versa?
    >
    > That's cool. Didn't know that before.


    Pretty much. I don't know what the performance hit though but it is handy.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Aug 4, 2007
    #11
  12. xz

    chucky Guest

    On Aug 4, 6:55 am, Knute Johnson <>
    wrote:
    > xz wrote:
    > > Do you mean now wherever you can use Integer, you can use int, and
    > > vice versa?

    >
    > > That's cool. Didn't know that before.

    >
    > Pretty much. I don't know what the performance hit though but it is handy.
    >


    I'd say that it is as fast as converting it "by hand", since the
    distinction between int and Integer can be done by compiler at compile
    time, so there is no runtime overhead.
     
    chucky, Aug 4, 2007
    #12
  13. xz

    Roedy Green Guest

    >anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
    >anotherArrayList.get(0).makeSomeChange();
    >Will the change made in the second line make effect on
    >oneArrayList.get(0)?

    You have watched Star Trek too many times. The computer will not
    explode if you try the experiment and guess incorrectly.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Aug 4, 2007
    #13
  14. xz

    xz Guest

    On Aug 4, 6:53 am, Roedy Green <>
    wrote:
    > >anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
    > >anotherArrayList.get(0).makeSomeChange();
    > >Will the change made in the second line make effect on
    > >oneArrayList.get(0)?

    >
    > You have watched Star Trek too many times. The computer will not
    > explode if you try the experiment and guess incorrectly.
    > --
    > Roedy Green Canadian Mind Products
    > The Java Glossaryhttp://mindprod.com


    Excuse me. Star Trek?
     
    xz, Aug 4, 2007
    #14
  15. xz

    Lew Guest

    Re: Does the clone() method of ArrayList<> make a copy of the objectsin the ArrayList?

    xz wrote:
    >>> anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
    >>> anotherArrayList.get(0).makeSomeChange();
    >>> Will the change made in the second line make effect on
    >>> oneArrayList.get(0)?


    Roedy Green wrote:
    >> You have watched Star Trek too many times. The computer will not
    >> explode if you try the experiment and guess incorrectly.


    xz wrote:
    > Excuse me. Star Trek?


    Or any other science-fiction work which uses the cliché of a computer
    exploding by feeding it bad data or code.

    He's telling you that you should've tried it. Implicitly he's inviting you to
    examine your actions and ponder the virtues of experimentation as a learning
    tool generally. The computer will answer your questions more accurately, more
    ineluctably and more convincingly than anything we can say.

    --
    Lew
     
    Lew, Aug 4, 2007
    #15
  16. Lew wrote:
    > xz wrote:
    >>>> anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
    >>>> anotherArrayList.get(0).makeSomeChange();
    >>>> Will the change made in the second line make effect on
    >>>> oneArrayList.get(0)?

    >
    > Roedy Green wrote:
    >>> You have watched Star Trek too many times. The computer will not
    >>> explode if you try the experiment and guess incorrectly.

    >
    > xz wrote:
    >> Excuse me. Star Trek?

    >
    > Or any other science-fiction work which uses the cliché of a computer
    > exploding by feeding it bad data or code.


    Or even non-SF. There was en episode of "The Paper Chase" (generally a
    fine, intelligent series) that used this same stupid cliche. There was a
    "super-computer" [1] that had been programmed to solve legal issues,
    threatening to make lawyers obsolete. But clever Professor Kingsfield asked
    it a hypothetical question, whereupon the tape drives whirred furiously, the
    lights dimmed, sparks began to fly, and the computer more or less blew up.

    1. Which was every bit as silly as the plot; it was built from racks and
    racks of obsolete PDP-11 peripherals.
     
    Mike Schilling, Aug 4, 2007
    #16
  17. xz

    xz Guest

    On Aug 4, 2:04 pm, Lew <> wrote:
    > xz wrote:
    > >>> anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
    > >>> anotherArrayList.get(0).makeSomeChange();
    > >>> Will the change made in the second line make effect on
    > >>> oneArrayList.get(0)?

    > Roedy Green wrote:
    > >> You have watched Star Trek too many times. The computer will not
    > >> explode if you try the experiment and guess incorrectly.

    > xz wrote:
    > > Excuse me. Star Trek?

    >
    > Or any other science-fiction work which uses the cliché of a computer
    > exploding by feeding it bad data or code.
    >
    > He's telling you that you should've tried it. Implicitly he's inviting you to
    > examine your actions and ponder the virtues of experimentation as a learning
    > tool generally. The computer will answer your questions more accurately, more
    > ineluctably and more convincingly than anything we can say.
    >
    > --
    > Lew


    :)
    got it.
    looks like I need more sense of humor:)
     
    xz, Aug 4, 2007
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary Newell

    Java 5, how to clone an ArrayList

    Gary Newell, Dec 8, 2004, in forum: Java
    Replies:
    1
    Views:
    10,082
    Gary Newell
    Dec 8, 2004
  2. lordy

    To clone or not to clone..

    lordy, Jul 7, 2006, in forum: Java
    Replies:
    3
    Views:
    729
    lordy
    Jul 7, 2006
  3. Tom Anderson
    Replies:
    1
    Views:
    391
  4. Roedy Green
    Replies:
    5
    Views:
    443
    John B. Matthews
    Dec 18, 2009
  5. albert kao
    Replies:
    10
    Views:
    3,111
    Eric Sosman
    Mar 5, 2011
Loading...

Share This Page