Overriding equal/hashCode

Joined
May 9, 2006
Messages
1
Reaction score
0
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,352
Latest member
SherriePet

Latest Threads

Top