Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Hash Table Problems

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 26957
84
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic