View Single Post
Old Sep 18th, 2007, 9:05 PM   #7
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
The cause of your problem is the fact that the clone method of a 2D array creates a shallow copy. (Stupid? Yes.) Think of a 2D array as an object with X references to Y 1D arrays (each of which contain additional references--the actual objects stored in the array). The clone method of a 2D array creates a new 2D array--but copies the references to the 1D arrays rather than cloning them! You can avoid this by writing your own method to copy a 2D array:

public static int[][] copy(int[][] a) {
    int[][] b = new int[a[0].length][a.length];
    for (int i = 0; i < a.length; i++)
        for (int j = 0; j < a[0].length; j++)
            b[i][j] = a[i][j];
    return b;
}
Hopefully that will solve your problem.
titaniumdecoy is online now   Reply With Quote