confusion of array clone() method

H

hyena

hi,

I just came across this situation that the cloned array linked to the
original array and changes made to the clone also applied to the original
one. I do not think I understand fully the description of deep clone/shallow
clone concept and got a bit confused here.

code:
private int[][] decode(int[][] tr_seq) {
// translate the sequces into real travel times
int[][] tt = (int[][]) tr_seq.clone();
for(int i =0; i < tt.length; i++){
for(int j =0; j < tt[0].length; j++){
tt[j] = 100;
}
}
return tt;
}

tr_seq was changed after this method was called. I was assuming a clone is
apart from the original object and has its own reference address. isn't it?

from java document ,

"Creates and returns a copy of this object. The precise meaning of "copy"
may depend on the class of the object. The general intent is that, for any
object x, the expression:
x.clone() != xwill be true".

this statement does not agree on this example.

could someone shed some light on this? Thanks.

Sun
 
B

Bob

you can find out what clone() does to arrays by adding an assertion:
int[][] tt = (int[][]) tr_seq.clone();
assert( tt != tr_seq );

if clone doesn't do the job for arrays, you can write your own
copy_array static method or see if there already is such a thing in the
Java API.
 
P

Patricia Shanahan

hyena said:
hi,

I just came across this situation that the cloned array linked to the
original array and changes made to the clone also applied to the original
one. I do not think I understand fully the description of deep clone/shallow
clone concept and got a bit confused here.

code:
private int[][] decode(int[][] tr_seq) {
// translate the sequces into real travel times
int[][] tt = (int[][]) tr_seq.clone();
for(int i =0; i < tt.length; i++){
for(int j =0; j < tt[0].length; j++){
tt[j] = 100;
}
}
return tt;
}

tr_seq was changed after this method was called. I was assuming a clone is
apart from the original object and has its own reference address. isn't it?

from java document ,

"Creates and returns a copy of this object. The precise meaning of "copy"
may depend on the class of the object. The general intent is that, for any
object x, the expression:
x.clone() != xwill be true".

this statement does not agree on this example.

could someone shed some light on this? Thanks.


The key issue is what constitutes an array. Java does not really have
multidimensional arrays. tr_seq is an array whose elements are
references to arrays.

clone makes a new array whose elements point to the same arrays as the
original.

For any i from 0 to tt.length-1, tt and tr_seq are identical. They
are either both null, or are pointers to the same array. Changes to an
element of an array they both reference apply regardless of whether you
find that array through tr_seq or tt.

Patricia
 
P

Patricia Shanahan

Patricia said:
hyena said:
hi,

I just came across this situation that the cloned array linked to
the original array and changes made to the clone also applied to the
original one. I do not think I understand fully the description of
deep clone/shallow clone concept and got a bit confused here.

code:
private int[][] decode(int[][] tr_seq) {
// translate the sequces into real travel times
int[][] tt = (int[][]) tr_seq.clone();
for(int i =0; i < tt.length; i++){
for(int j =0; j < tt[0].length; j++){
tt[j] = 100;
}
}
return tt;
}

tr_seq was changed after this method was called. I was assuming a
clone is apart from the original object and has its own reference
address. isn't it?

from java document ,

"Creates and returns a copy of this object. The precise meaning of
"copy" may depend on the class of the object. The general intent is
that, for any object x, the expression:
x.clone() != xwill be true".

this statement does not agree on this example.

could someone shed some light on this? Thanks.


The key issue is what constitutes an array. Java does not really have
multidimensional arrays. tr_seq is an array whose elements are
references to arrays.

clone makes a new array whose elements point to the same arrays as the
original.

For any i from 0 to tt.length-1, tt and tr_seq are identical. They
are either both null, or are pointers to the same array. Changes to an
element of an array they both reference apply regardless of whether you
find that array through tr_seq or tt.


Rereading this, I saw an ambiguity. tt and tr_seq are identical
immediately after the clone call. An assignment e.g. tt = null; could
make them different.

Patricia
 
H

hyena

Patricia Shanahan said:
hyena wrote:
this statement does not agree on this example.

could someone shed some light on this? Thanks.

The key issue is what constitutes an array. Java does not really have
multidimensional arrays. tr_seq is an array whose elements are
references to arrays.

clone makes a new array whose elements point to the same arrays as the
original.

For any i from 0 to tt.length-1, tt and tr_seq are identical. They
are either both null, or are pointers to the same array. Changes to an
element of an array they both reference apply regardless of whether you
find that array through tr_seq or tt.

Patricia



Thanks Patricia, and the others. This explaination makes the idea clear to
me.

thanks!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top