It's not a secret anymore!
The moose likes Beginning Java and the fly likes HashTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashTable" Watch "HashTable" New topic


Shilong Zheng

Joined: May 08, 2004
Posts: 29
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.

Darin Niard
Ranch Hand

Joined: Jun 08, 2004
Posts: 118
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 ]
Tony Jackson
Ranch Hand

Joined: Feb 23, 2001
Posts: 45

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.

[ June 21, 2004: Message edited by: Tony Jackson ]
Shilong Zheng

Joined: May 08, 2004
Posts: 29
Thanks a million Tony, you are my saviour. Appreciate your examples very much. Helps to understand and see the big picture. Thanks once again!!!

sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
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.

I agree. Here's the link:
subject: HashTable
It's not a secret anymore!