Hi
I'm quite new to Java and i've some trouble overriding equals/hashcode, and there need some help
I've two arrays of int:
public static int[][] A = {{0,1,2},{34,5,7}};
public static int[][] B = {{0,1,2},{34,5,7}};
I want to override equals and hashCode to check if the arrays are alike, but i'm having some trouble. This is my code:
public boolean equals(Object obj){
if(A.length != B.length) return false;
if(A.length == B.length) {
for(int i=0; i<A.length; i++){
for (int j=0;j<A[0].length;j++)
if(A[j] != B[j]) {
return false;
}
}
}return true;
}
public int hashCode() {
<i have no idea how to write this>
return result ;
}
public void testArr() {
System.out.println("Array A = "+A.hashCode());
System.out.println("Array B = "+B.hashCode());
assertEquals(A,B);
Please, could anyone help me with the missing code?
Thanks, Peter
I'm quite new to Java and i've some trouble overriding equals/hashcode, and there need some help
I've two arrays of int:
public static int[][] A = {{0,1,2},{34,5,7}};
public static int[][] B = {{0,1,2},{34,5,7}};
I want to override equals and hashCode to check if the arrays are alike, but i'm having some trouble. This is my code:
public boolean equals(Object obj){
if(A.length != B.length) return false;
if(A.length == B.length) {
for(int i=0; i<A.length; i++){
for (int j=0;j<A[0].length;j++)
if(A[j] != B[j]) {
return false;
}
}
}return true;
}
public int hashCode() {
<i have no idea how to write this>
return result ;
}
public void testArr() {
System.out.println("Array A = "+A.hashCode());
System.out.println("Array B = "+B.hashCode());
assertEquals(A,B);
Please, could anyone help me with the missing code?
Thanks, Peter