1

I'm writing a program using A* for a computer class and cannot seem to get the contains method to function how I need it to. I am using a pair of Priority Queues to hold objects of a Node class, which I defined. I wrote an equals method in the Node class, and it works fine on an individual basis, and, as far as I can tell, it will also override the base equals method from the Object class, when I call it by itself.

Calling the contains method though, will not detect Nodes that are the "same". I was under the impression that contains uses equals to determine if the Queue does indeed contain the object in question. I've messed with it, and have become quite stumped as to the reason, leading me to believe that my understanding of the way contains works is flawed, but, as far as I can tell by the Java documentation, it looks like it should work.

Any suggestions or pointers in the right direction would be greatly appreciated, thanks.

Ladlestein
  • 6,100
  • 2
  • 37
  • 49
  • 2
    Would be helpful to see some code examples, but consider that you need to implement hashCode as well as equals, and they need to be consistent with one another – Ian McMahon Feb 26 '13 at 02:52
  • Java contains() method is very simple : if (o != null) { for (int i = 0; i < size; i++) if (o.equals(queue[i])) return i; } return -1; } – user1428716 Feb 26 '13 at 02:57
  • 1
    Thank you very much. I had neglected to implement hashCode... Feeling like an idiot for overlooking it, but equally relieved that it seems to be in order now. – Ryan Leonard Feb 26 '13 at 03:23

0 Answers0