Cloning multi-dimensional arrays

I am working on this program of mine and really need some help since I am completely stuck, the thing is that I am trying toclone a multi-dimensional array, I have tried with a simple array and that worked fine, but not with the multi-dimensional. Like this:

int[] initialSquareValues3 ={1,2,3};

int[] squareValues3 = initialSquareValues3.clone();

System.out.println(initialSquareValues3[0]);

System.out.println(squareValues3[0]);

squareValues3[0] = 3;

System.out.println(initialSquareValues3[0]);

System.out.println(squareValues3[0]);

Will give the output "1 1 1 3", just what i want. While this:

int[][] initialSquareValues2 ={{1,2,3},{4,5,6},{7,8,9}};

int[][] squareValues2 = initialSquareValues2.clone();

System.out.println(initialSquareValues2[0][0]);

System.out.println(squareValues2[0][0]);

squareValues2[0][0] = 3;

System.out.println(initialSquareValues2[0][0]);

System.out.println(squareValues2[0][0]);

Will give the output "1 1 3 3" where "squareValues2" only seems to be a reference to the same data as the original "initialSquareValues2"

So the question is simply:How do i clone a multi-dimensional array?

I'm running Java SE 5.0 (1.5.0) and the code is run inside the contructor in a class extending JPanel.

[1762 byte] By [Lizzarda] at [2007-11-27 1:07:04]
# 1

Try writing your own method instead.

public int[][] poo(int[][] a] {

int[][] newArr = new int[a's row length][a's column length];

for(while r is less than a.length)

for(while c is less than a[0].length)

newArr[row][col] = a[row][col];

return newArr;

}

lethalwirea at 2007-7-11 23:42:17 > top of Java-index,Java Essentials,Java Programming...
# 2
Yep, that worked. Thanks... should have thought of that...Well anyway, how come you can't .clone() a multi-dimensional array, that is, if you can't of course.And again, thanks for the help and the quick reply.
Lizzarda at 2007-7-11 23:42:17 > top of Java-index,Java Essentials,Java Programming...
# 3

No need to explicitly do any copying of elements except as part of deepening the clone.

public int[][] cloneIntArrays(int[][] array)

{

// Create shallow clone

int[][] clonedArray = array.clone();

// Deepen the shallow clone

for (int index = 0; index < clonedArray.length; index++)

clonedArray[index] = clonedArray[index].clone();

return clonedArray;

}

sabre150a at 2007-7-11 23:42:17 > top of Java-index,Java Essentials,Java Programming...