I have some problems understanding the behavior of Arrays.deepHashCode
on int[][]
.
I assume the program is easy enough to read. A and B has equal hash as expected. D is also different from the rest. But for some reason C is equal to A and B. C is actually transpose of arrayA (and arrayB), but that may just be a coincidence and not the reason for same hash code returned.
Am I not supposed to use this method on int[][]
? Any suggestions of a good way to make my own hashCode for int[][]
that returns equal integer if and only if each element in each subarray are equal to each other (same position, same length...like deep cloned)
import java.util.Arrays;
public class Test{
public static void main(String[] args){
int[][] arrayA = new int[][]{
{1,2,3},
{4,5,6},
{7,8,9}
};
int[][] arrayB = new int[][]{
{1,2,3},
{4,5,6},
{7,8,9}
};
int[][] arrayC = new int[][]{
{1,4,7},
{2,5,8},
{3,6,9}
};
int[][] arrayD = new int[][]{
{1,5,9},
{2,3,7},
{4,6,8}
};
System.out.println("Deep hash codes:");
System.out.println("Array A: " + Arrays.deepHashCode(arrayA));
System.out.println("Array B: " + Arrays.deepHashCode(arrayB));
System.out.println("Array C: " + Arrays.deepHashCode(arrayC));
System.out.println("Array D: " + Arrays.deepHashCode(arrayD));
}
}
Output:
Deep hash codes:
Array A: 30729379
Array B: 30729379
Array C: 30729379
Array D: 30760099