Java, and Cloning Arrays

Java-Evil-Edition-orfjackal_netJust to let you know, in case you find this kind of problem… Java is able to clone uni-dimensional arrays of built-in types, like int, but it is not able to clone multidimensional arrays, as a multidimensional array is an array of arrays, and an array is an object. Not sure this is the explanation, but the truth is… clone is useless in this evil language.

If you want to prove it yourself, just try the following code.

public class TestClone {

    public static void main(String[] args) {

    	int[][] boardM = new int[1][1];
    	int[][] copyM;

    	boardM[0][0] = 1;
    	copyM = boardM.clone();

    	boardM[0][0] = 2;

    	if (copyM[0][0] == boardM[0][0]) {
    		System.err.println("oops?");
    	}

    	int[] boardV = new int[1];
    	int[] copyV;

    	boardV[0] = 1;
    	copyV = boardV.clone();

    	boardV[0] = 2;

    	if (copyV[0] == boardV[0]) {
    		System.err.println("oops, too?");
    	}
    }
}