posted 15 years ago
Greetings everyone!
My problem is the following:
I have a Node class which now represents a cell in a matrix. It implements the Comparable interface and overrides the equals(...) method because I'd like to store Node objects in a TreeSet<Node> set.
Unpredicted behaviour occurs at this point:
I'll explain it properly. openedNode and n are instances of Node, openedNodes is an instance of TreeSet<Node>.
The method g(Node, Node) in the first row of the code above just adds the g values of the two parameters, getG() and setG(int) are just regular get and set methods.
When execution reaches "openedNodes.add(openedNode);" sometimes it says that openedNodes already contains openedNode (but in reality, it doesn't contains it).
Then I tried this:
In this case it removes a Node from openedNodes (call it falseOpenedNode) and then adds the actual openedNode, so it finds that these two nodes are equivalent. But when I call openedNode.equals(falseOpenedNode) explicitly at this point it returns false (which is correct).
I'll show an example with concrete values if you need it.
Any help is appreciated! Thank you.