...

View Full Version : hashmap but storing 2 values with an identifier



nickyfraggle
04-19-2008, 05:03 PM
Hello,

I would like the functionality of a hashmap but I have 3 values to store, one of which would be an identifier to search for and 2 other values.

From what I can see, hashmaps only allow 1 value to be stored.

Is there something with the same functionality? I need to be able to search by an ID.

Thanks,

Nicola

shyam
04-19-2008, 05:25 PM
if the other 2 values are of the same type then u can put them in the hashmap as an array


// assuming strings
String[] values = new String[] {"key", "value1", "value2"};
HashMap<String, String[]> map = new HashMap<String, String[]>();
map.put(values[0], new String[]{values[1], values[2]});

or if they are of dissimilar types you can create a holding class


class Info {
String key;
int intValue;
double double Value;
}
...

Info info = new Info();
info.key = "key";
HashMap<String, Info> map = new HashMap<String, Info>();
map.put(info.key, info);

nickyfraggle
04-19-2008, 06:42 PM
Hello,

I think I might be going about this in the wrong way, so I'm going to try to explain what I'm trying to do.

I am drawing directed graphs from set theory files.

A file is opened, and then a graph is drawn based on it's contents.

Every time a circle is drawn, I want to store:

an X and Y co-ordinate of where it started drawing it (top left X,Y coord) setName (I get this using a method)
Location (I get this using a method)

Users will click on the circles in my graph to open a new set theory file, using it's location and setName, so I am getting the XY coordinates of where they clicked, and then need to check it against the values I've stored to find out which setName circle they clicked on.

Each circle drawn is 20X20, so I'll just be looking for a user clicking within 20 of the initial coordinates.

I thought about saving these values in an instance of a class, and then storing that instance in a Vector (as more than one circle might be drawn on the screen at one time.) BUT....

When a user clicks a) how do I know which instance of the class I'm looking for.....and how do I search the instance stored inside the vector to decide if a user has clicked within the circle of a particular instance?? (remember each instance is a new circle with it's XY start coords stored)

I'm hoping I'm making some sense here!

REALLY appreciate your help. I'm a final year honours student and have been banging my head against a wall with this for a little while!!! I don't need to be told line for line what to do...I just don't know where to start.

Nicky

shyam
04-19-2008, 07:48 PM
Each circle drawn is 20X20, so I'll just be looking for a user clicking within 20 of the initial coordinates.

I thought about saving these values in an instance of a class, and then storing that instance in a Vector (as more than one circle might be drawn on the screen at one time.) BUT....

When a user clicks a) how do I know which instance of the class I'm looking for.....and how do I search the instance stored inside the vector to decide if a user has clicked within the circle of a particular instance?? (remember each instance is a new circle with it's XY start coords stored)

well, you've solved your own problem...you have a vector that contains objects of a class that have x & y coordinates of the circle, location and name...now when a user clicks somewhere you get the x & y coordinates of the mouse click...all that is left to do is to iterate through the elements in the vector and calculate the distance between the center of your circles (x-10, y-10) assuming 10 is your radius and the mouse click...if the distance is less than the radius (10) then the user has clicked inside that particular instance's circle and you use that one for further processing...

jon_russ_lover
04-20-2008, 09:14 PM
Hello ! I also have a similar problem.

I need to implement a multiple choice assessment system, but I am at the stage of reading in the values from a text file. So far I have been reading them (using a tab delimited file) where each string is placed into a different variable.

The problem is I would like to store the Question/Answers in a hashmap like this :

ID (as the key which is randomly generated)

Question (Which is a seperate class type (containing the question text)

Answers : These consist of 4 possible answers (Which are of type Question) these hold the text of the answer and also a boolean value where it is the correct answer or not.

My problem is that I don't know how to place the answers, question and id into the hashmap.

After reading the previous post, I see that you could create a holding class. Would this be in my case of type QA (Questions and Answers)?

class QA {
int key;
Question q;
Answers [Answer];

If so how would I go about placing my 4 answer types into the answers part of the collection????

Hope this makes sense.
Thanks

nickyfraggle
04-20-2008, 09:26 PM
Hello,

I wrote a multiple choice question program last year.
I used Vectors to store the questions and answers.

Have you looked at these?

Nicky

nickyfraggle
04-20-2008, 09:30 PM
Not got my computer on me at the moment as it decided to just not bother starting up last week and is still being fixed but I can send you some stuff to look at tomorrow if you're still stuck!

jon_russ_lover
04-20-2008, 09:32 PM
No I haven't looked at those so will have a search on the internet in a bit !

Thats great thanks, I would much appreciate it if I could view it? But only if its not too much trouble for you.

I will pm my email address to you,
Thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum