different hashcodes being generated on equal arrays
I have a recursive program that is traversing a tree and it is using hashcode() to generate keys for a hashtable to prevent repeated states. The code is posted below.
What is happening it that it appears that two arrays with the exact same contents are producing different hashcodes, even though the API states that arrays that have the same contents will generate the same hashcode, or am I just interpreting this wrong?
The code (for the main part of the program):
package main;
import java.util.*;
public class DFS {
StatePos positions = new StatePos();
int depth = 0;
public State doDFS(State initialState, State goalState) {
return recurseDFS(initialState, goalState);
}
public State recurseDFS(State currState, State goalState) {
depth++;
ArrayList<State> successors;
System.out.print("entered - ");
currState.printState();
System.out.println("Hashcode - " + currState.stateArr.hashCode());
for (Integer temp:currState.stateArr) {
System.out.print(temp);
}
System.out.println(":");
if (positions.closed.contains(currState.stateArr.hashCode())) {
// System.out.print("closed - ");
// currState.printState();
return null;
}
positions.addToClosed(currState);
// System.out.print("processing - ");
// currState.printState();
if (currState.stateArr.equals(goalState.stateArr)) {
currState.printState();
return currState;
}
successors = positions.expand(currState);
for (State successor:successors) {
recurseDFS(successor, goalState);
}
depth--;
return null;
}
}
and the output for the first few iterations:
entered - 1,2,3,4,5,*,8,7,6,
Hashcode - 11423854
123450876:
entered - 1,2,*,4,5,3,8,7,6,
Hashcode - 11646147
120453876:
entered - 1,*,2,4,5,3,8,7,6,
Hashcode - 22052786
102453876:
entered - *,1,2,4,5,3,8,7,6,
Hashcode - 8984226
012453876:
entered - 1,*,2,4,5,3,8,7,6,
Hashcode - 21539363
102453876:
As you can see, the 3rd and 5th ones look exactly the same with the array contents, but the hashcodes are different. What's up?
Thanks,
Jim
[2314 byte] By [
jlw23a] at [2007-10-3 6:32:52]

That may be, but the API states for equals() that:
"Returns true if the two specified arrays of ints are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null."
then for hashcode:
"Returns a hash code based on the contents of the specified array. For any two non-null int arrays a and b such that Arrays.equals(a, b), it is also the case that Arrays.hashCode(a) == Arrays.hashCode(b). "
Also, I did a test. I created two completely different array objects (different declarations and different instances) with the exact same contents, called hashcode on them and they came back with the exact same hashcode. The problem lies somewhere else (maybe with recursion?), and it is probably with my code, but I have no idea what. Just looking for suggestions.
BTW - stateArr is an array of int and currState is an object that containts stateArr. Those are declared in a different class file.
Jim
> That may be, but the API states for equals() that:
>
> "Returns true if the two specified arrays of ints are
> equal to one another. Two arrays are considered equal
> if both arrays contain the same number of elements,
> and all corresponding pairs of elements in the two
> arrays are equal. In other words, two arrays are
> equal if they contain the same elements in the same
> order. Also, two array references are considered
> equal if both are null."
>
> then for hashcode:
>
> "Returns a hash code based on the contents of the
> specified array. For any two non-null int arrays a
> and b such that Arrays.equals(a, b), it is also the
> case that Arrays.hashCode(a) == Arrays.hashCode(b).
> "
Sorry, now I'm lost. You are citing the documentation of java.util.Arrays.equals and java.util.Arrays.hashCode.
Those are the methods that I suggested you should use instead of the member functions equals and hashCode.
So, maybe you were using them all along, and I got it all wrong.