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


X

xz

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

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

Advertisements

?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

xz said:
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
 
K

Knute Johnson

Arne said:
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
 
T

Twisted

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

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

Patricia Shanahan

Knute said:
Arne said:
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
 
X

xz

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

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?
 
Ad

Advertisements

?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

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?=

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
 
X

xz

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

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

Knute Johnson

Arne said:
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.
 
K

Knute Johnson

xz said:
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.
 
Ad

Advertisements

C

chucky

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

Roedy Green

anotherArrayList = (ArrayList said:
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.
 
L

Lew

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

Mike Schilling

Lew said:
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.
 
Ad

Advertisements

X

xz

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.

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

Top