Win a copy of Svelte and Sapper in Action this week in the JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

HashTable

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hashtable stores information in the form of a key and value..
If i want to associate different marks of assignments to different names,
it will have variables like studentName, assignmentNo and mark. Kindly advise how do i structure the hashtable to store and retrieve the correct marks belonging to a certain assignmentNo and certain studentName. Would appreciate if examples is provided... thank you in advance...
Say every student have 3 assignments.

Regards
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, there are many different ways to do this. One way would be to have an array of all of the assignments as the hash table value, so that the student's name would be associated with a double[] for instance, and the index of the array would be the assignment number. Alternatively, you can use another hashtable or any other ADT.
[ June 21, 2004: Message edited by: Darin Niard ]
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shilong,

It sounds like you want to store a number of things in each hashtable entry.

First things first. Here is some sample code that creates a Hashtable, puts in a couple of entries, and then retrieves those entries:



OK, now what if you have multiple grades per student? You can store an array in the Hashtable for each entry, instead of storing a single Integer object:



I hope that helps.

Tony
[ June 21, 2004: Message edited by: Tony Jackson ]
 
Shilong Zheng
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a million Tony, you are my saviour. Appreciate your examples very much. Helps to understand and see the big picture. Thanks once again!!!


Cheers
Shilong
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The better approach would be to create an object that has the internal structure you'd like to keep for all of this information. What is the association between a student and their grades? You might decide that a student should have a class history, for example. So you write a Class object that represents a single class, has properties like professor, title, department, etc. Then an array representing the class history, each item of which might be a ClassHistoryItem, which is a reference to the class, the grade earned, the term in which the class was taken.

This is just a very rough first cut, but you can see that how you structure information is what makes OOP powerful:



Now you can have a HashMap (don't use Hashtable anymore, that's an old class from the days of the JDK 1.1) that stores student objects and associates them with a student id, or their name, or whatever info you want to use as the key.

sev
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic