Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
xz - 03 Aug 2007 20:56 GMT
anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
anotherArrayList.get(0).makeSomeChange();

Will the change made in the second line make effect on
oneArrayList.get(0)?
Arne Vajhøj - 03 Aug 2007 21:04 GMT
> 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
Knute Johnson - 04 Aug 2007 01:28 GMT
>> anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
>> anotherArrayList.get(0).makeSomeChange();
[quoted text clipped - 5 lines]
>
> 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

Signature

Knute Johnson
email s/nospam/knute/

Twisted - 04 Aug 2007 01:57 GMT
On Aug 3, 8:28 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> Arne Vajh?j wrote:
> >> anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
[quoted text clipped - 25 lines]
>          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.
Patricia Shanahan - 04 Aug 2007 02:03 GMT
>>> anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
>>> anotherArrayList.get(0).makeSomeChange();
[quoted text clipped - 35 lines]
> 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
xz - 04 Aug 2007 04:06 GMT
On Aug 3, 7:28 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> Arne Vajh?j wrote:
> >> anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
[quoted text clipped - 23 lines]
>          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++)
[quoted text clipped - 9 lines]
> 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?
Arne Vajhøj - 04 Aug 2007 04:11 GMT
>>          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 - 04 Aug 2007 04:45 GMT
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 <a...@vajhoej.dk> wrote:
> >>          ArrayList<Integer> list1 = new ArrayList<Integer>();
> >>          list1.add(10);
[quoted text clipped - 6 lines]
>
> Arne
Knute Johnson - 04 Aug 2007 05:55 GMT
> 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.

Signature

Knute Johnson
email s/nospam/knute/

chucky - 04 Aug 2007 11:23 GMT
On Aug 4, 6:55 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
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.
Arne Vajhøj - 04 Aug 2007 04:13 GMT
>> 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
Knute Johnson - 04 Aug 2007 05:54 GMT
>>> clone is a shallow clone not a deep clone so yes.
>
[quoted text clipped - 9 lines]
>
> 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.

Signature

Knute Johnson
email s/nospam/knute/

Roedy Green - 04 Aug 2007 12:53 GMT
>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.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

xz - 04 Aug 2007 18:29 GMT
On Aug 4, 6:53 am, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> >anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
> >anotherArrayList.get(0).makeSomeChange();
[quoted text clipped - 6 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com

Excuse me. Star Trek?
Lew - 04 Aug 2007 20:04 GMT
>>> 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.

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

Signature

Lew

Mike Schilling - 04 Aug 2007 20:49 GMT
>>>> anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
>>>> anotherArrayList.get(0).makeSomeChange();
[quoted text clipped - 9 lines]
> 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.
xz - 04 Aug 2007 23:33 GMT
> >>> anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
> >>> anotherArrayList.get(0).makeSomeChange();
[quoted text clipped - 15 lines]
> --
> Lew

:)
got it.
looks like I need more sense of humor:)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.