Results 1 to 10 of 10
- 04-24-2012, 08:20 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
Help with creating a HashMap from Arrays
Hi folks,
I'm having a lot of trouble in figuring out this homework question so any advice/pointers is much appreciated.
Here goes...
This
is the lists that I need to put into a Map as key value pairs. They pair with each other helicopter = greg, will etc/ Amphibious = Yuri, Abe etcXML Code:String[] vehicles = {"Helicopter", "Amphibious","4x4", "Fire Engine"}; String[][] whoDrivesWhat = { {"Greg", "Will", "Fleur", "Bill", "Bella", "Sally", "Olive", "Sal", "Dora", "Chas"}, {"Yuri", "Abe", "Tim", "Fleur", "Bonnie", "Vera", "Ed", "Dan", "Jill", "Rose", "Zoe"}, {"Tim", "Fleur", "Will", "Bill", "Bella", "Dan", "Jill", "Rose", "Greg", "Abe", "Sally", "Yuri", "Olive", "Sal", "Jim"}, {"Jill", "Rose", "Greg", "Abe", "Bonnie", "Vera", "Ed", "Zoe", "Ben", "Freda", "Chuck", "Fred"}
The constructor in my code should be this I think
and my code for the method is as followsXML Code:qualifiedDrivers = new HashMap<String, ArrayList<String>>();
My code is not quite right obviously. I need to iterate over vehicles and then an appropriate row of whoDrivesWhat to satisfy the question. I then need to input this as key value-pairs into the Map but i'm not sure how to do this.XML Code:public void populateQualifiedDrivers(String[] vehicles, String[][] whoDrivesWhat) { ArrayList<String> drivers; for (int i = 0; i < vehicles.length; i++) { drivers = new ArrayList<String>(); for (int j = 0; j < whoDrivesWhat[i].length; j++) { qualifiedDrivers.put(vehicles[i], drivers); } } }
Please help ;)
- 04-24-2012, 08:32 PM #2
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: Help with creating a HashMap from Arrays
So for your HashMap, the vehicles are the keys and the list of drivers for that vehicle is the data associated with the key right? You are on the right track with your thinking.
You establish the vehicle as the key in your outer loop. In your inner loop you are trying to create the ArrayList for the drivers that go with the vehicle. You don't want to create your HashMap entry (i.e. the qualifiedDrivers.put(vehicles[i], drivers);) until AFTER you create the drivers ArrayList that you are trying to put into the HashMap.
So giving you that much, what do you think you need to do? Should that put be where it is? What aren't you doing with the drivers ArrayList before you do the put?
- 04-24-2012, 08:59 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
Re: Help with creating a HashMap from Arrays
Hi and thanks for the quick reply.
I think I need to add each driver to drivers before the inner loop finishes - this is what I'm unsure on - Do i need to populate the arrayList 1st?
Regards
- 04-24-2012, 09:46 PM #4
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: Help with creating a HashMap from Arrays
The most common and simple approach is to add the drivers to the ArrayList in the inner loop and to put that newly created ArrayList into your HashMap after it has been completed.
Look carefully at your loop constructs and what is being done within each of them. If you leave the put where you currently have it, what do you think is going to happen?
Edit - If you have updated code to post, please post a NEW reply with it. Don't edit the first post to update the code listed there please.
- 04-24-2012, 09:57 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
Re: Help with creating a HashMap from Arrays
ok thanks again.
This is what the question asks me to do...
• First your method should declare a local variable called drivers capable of
referencing a collection compatible with the value type declared for
qualifiedDrivers.
• An outer loop should then iterate over vehicles.
o Within the loop you should you should assign an instance of an
appropriate collection class to drivers.
o An inner loop should iterate over an appropriate row of
whoDrivesWhat, adding each driver to drivers.
o After the inner loop ends the current vehicle and drivers should be
added to qualifiedDrivers as a key/value pair.
Any more advice at all based on this?
Regards
- 04-24-2012, 10:05 PM #6
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: Help with creating a HashMap from Arrays
Nope, just think about what I said and do the exercise. Do post back if you have further questions.
- 04-24-2012, 10:07 PM #7
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
Re: Help with creating a HashMap from Arrays
ok - got it now.
This seems to work...
The penny finally dropped lol - muchos gratzias ;)XML Code:public void populateQualifiedDrivers(String[] vehicles, String[][] whoDrivesWhat) { ArrayList<String> drivers; for (int i = 0; i < vehicles.length; i++) { drivers = new ArrayList<String>(); for (int j = 0; j < whoDrivesWhat[i].length; j++) { drivers.add(whoDrivesWhat[i][j]); qualifiedDrivers.put(vehicles[i], drivers); } } }
- 04-24-2012, 10:21 PM #8
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: Help with creating a HashMap from Arrays
Not quite. Why do you have the put in the inner loop? Do you want to create a HashMap entry for each driver?
To be honest, I'm not sure what HashMap will do under the covers, I don't have the time to go look it up for you. If you google on "Java HashMap" you should find plenty of API material to read up on just what put will do when do multiple put calls using the same key (vehicles[i] in this case).
- 04-24-2012, 10:29 PM #9
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
Re: Help with creating a HashMap from Arrays
I moved the 'put' to the very next line outside the inner loop and it gives the exact same results when creating the HashMap?
I'll google what you said for extra clarification though.
Thanks
- 04-24-2012, 10:39 PM #10
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Similar Threads
-
Problem creating arrays
By JohnPringle83 in forum New To JavaReplies: 7Last Post: 05-06-2011, 11:01 PM -
Creating generic arrays
By stijn1989 in forum Advanced JavaReplies: 6Last Post: 11-18-2009, 01:26 PM -
Creating a constructor with arrays and arguments - Part 2
By fullmetaljacket in forum New To JavaReplies: 15Last Post: 07-05-2009, 01:38 PM -
Creating a constructor with arrays and arguments
By fullmetaljacket in forum New To JavaReplies: 38Last Post: 07-03-2009, 06:09 AM -
[SOLVED] Creating an Array of Arrays?
By xcallmejudasx in forum Advanced JavaReplies: 5Last Post: 11-04-2008, 06:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks