Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Does the clone() method of ArrayList<> make a copy of the objects in the ArrayList?

Reply
Thread Tools

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

 
 
xz
Guest
Posts: n/a
 
      08-03-2007
anotherArrayList = (ArrayList<Something>) oneArrayList.clone();
anotherArrayList.get(0).makeSomeChange();

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

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      08-03-2007
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
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      08-04-2007
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/
 
Reply With Quote
 
Twisted
Guest
Posts: n/a
 
      08-04-2007
On Aug 3, 8:28 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
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.

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      08-04-2007
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
 
Reply With Quote
 
xz
Guest
Posts: n/a
 
      08-04-2007
On Aug 3, 7:28 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
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?



 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      08-04-2007
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
 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      08-04-2007
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
 
Reply With Quote
 
xz
Guest
Posts: n/a
 
      08-04-2007
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:
> 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



 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      08-04-2007
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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
Copy constructor: why can't I copy objects as if they were structs? rdc02271 C++ 24 12-27-2005 12:38 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
JRuby: How does one keep Java objects as Java objects so they can be used in method calls? Steve Drach Ruby 3 06-19-2004 11:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57