Reputation: 5193
I have a Vector<int[][]>
and when I give it a clone of my int[][]
array and than change something in my int[][]
array and call int[][] = Vector.get(0)
the array doesn't revert back to the value it should.
How can I get my int[][] to become the value of Vector.get(0)?
import java.util.Vector;
public class ArrayFromVectorProblem {
public static void main(String[] args){
int[][] myArray = {{0,0,1, 0, 0}, {0,0,2,0,0}};
Vector<int[][]> myVector = new Vector<int[][]>();
myVector.add(myArray.clone());
myArray[1][2] = 5;
//should print 5, and prints 5
System.out.println("MyArray[1][2]: " + myArray[1][2]);
myArray = myVector.get(0);
//should print 2, and prints 5
System.out.println("MyArray[1][2] 2nd try: " + myArray[1][2]);
}
}
this is my output:
MyArray[1][2]: 5
MyArray[1][2] 2nd try: 5
Upvotes: 0
Views: 68
Reputation: 725
Here the problem is that you have two types of arrays: One that contains objects, and severals that contain primitive types (int).
myArray is, if you want, not a multi-dimensional int array, but a one-dimensional object array that points at several one-dimensional int arrays (and an int array is an object). This is how it looks like inside your memory:
myArray1
0 -> intArray1
1 -> intArray2
with
intArray1 = [0, 0, 1, 0, 0]
intArray2 = [0, 0, 2, 0, 0]
So when you clone your myArray1, you get a myArray2 which is a separate copy:
myArray1
0 -> intArray1
1 -> intArray2
myArray2
0 -> intArray1
1 -> intArray2
but both copies of your myArray point at the same intArrays!
Here, you need to not only clone the outer array, but also the inner one, such that in the end you have a structure
myArray1
0 -> intArray1
1 -> intArray2
myArray2
0 -> intArray3
1 -> intArray4
with intArray3 a copy of intArray1 and intArray4 a copy of intArray2. You can do that either manually using loops, or google for java frameworks that help you do that (probably Google Guava, Apache Commons, or maybe there is even a helper class in the standard java libraries).
Upvotes: 1
Reputation: 10028
You are mis-understanding the clone method. Clone performs a shallow copy.
See this other stackoverflow article: Does calling clone() on an array also clone its contents?
This means that the only aspect of myVector.get(0) that is exclusive to the array is the outer array, that is, the first-dimension. The first dimension is a clone. However, the array itself is an array of references to integer arrays. References still point to the original objects.
Upvotes: 1
Reputation: 23015
Note that in myArray.clone()
is performing a shallow copy of the array.
The behaviour you are expecting is what you will get from a deep copy.
Check this post to see the difference: How do I copy an object in Java?
Upvotes: 3
Reputation: 347
In Java, cloning a multidimensional array performs a shallow copy. You'll get a new outer array, but the inner arrays will still be shared, and thus changes to the inner arrays will affect both the original outer array and its clone. If you want a deep copy, you need to either use the java.util.Arrays
utility class or do it manually.
Upvotes: 1