• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hash Table Problems  RSS feed

 
Eric Mitchell
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, so I have been trying to write a hash table class from scratch (as a programming exercise), but when it comes to testing, nothing is coming out right! For instance my "containsKey" method will return false no matter what, and my "get(key)" method can never find the keys. I assume it is all due to ONE central problem, but I can't find it. I have spent a lot of time looking at this, and I thank you in advance for your help.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not a bad first attempt, although there's a few glitches in there. However you haven't done enough testing -- yes, your code doesn't find Beethoven because of one of those glitches, but it should find those other two guys.

Let me just point out that when your put() method tries to add a new node to your linked list of nodes, there should be a line of code which looks something like "someNode.next = someOtherNode". You don't have any such line of code. Can you see what problem that would lead to?

By the way, the fields of your HashTable shouldn't be static. You won't notice any problem if you only have one HashTable, but as soon as you have two, all hell's going to break loose.
 
Eric Mitchell
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
alright, I got it! Yes, I attempted to write the statement someNode.next=someOtherNode, but had instead written newNode=occupiedNode.next (Line 43). I had also written in several places, "if (...key==otherKey)" like lines 92 & 105, when it should have been "key.equals(otherKey)". Also changed variables for list and count to be non-static, Not sure what I was thinking there...Anyways, thank you so much for your help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!