Reputation: 113
I have a 2-dimensional vector called 'link_occur_nodup' which contains data as [[5, 2, 1, 1], [2, 1, 1]]. Now, if i try to do
Vector<Vector<Integer>> temp=(Vector<Vector<Integer>>) link_occur_nodup.clone();
temp.elementAt(0).set(1, 50);
System.out.println(temp+" "+link_occur_nodup);
The output is: [[5, 50, 1, 1], [2, 1, 1]] [[5, 50, 1, 1], [2, 1, 1]] Iam wondering why does the value being changed in both of the vectors?. Instead it has to be only in the 'temp' vector. Can someone explain please?
Upvotes: 1
Views: 240
Reputation: 198014
clone
only does a shallow copy: so you get a new Vector
with references to the same objects as the original. This is the expected behavior.
If you want different behavior, you'll need to manually copy the inner Vector
s yourself. (This is one of the many reasons why the use of clone
is ill-advised.)
Upvotes: 12
Reputation: 887225
You've cloned the outer Vector.
Your cloned vector contains the same inner vectors as the original one.
Upvotes: 3