Results 1 to 10 of 10
  1. #1
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default 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
    XML 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"}
    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 etc

    The constructor in my code should be this I think
    XML Code:
    qualifiedDrivers = new HashMap<String, ArrayList<String>>();
    and my code for the method is as follows
    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);
          }
       }
    }
    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.

    Please help ;)

  2. #2
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default 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?

  3. #3
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default 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

  4. #4
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default 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.

  5. #5
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default 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

  6. #6
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default 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.

  7. #7
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Help with creating a HashMap from Arrays

    ok - got it now.
    This seems to work...
    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);
          }
       }
    }
    The penny finally dropped lol - muchos gratzias ;)

  8. #8
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default 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).

  9. #9
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default 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

  10. #10
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Help with creating a HashMap from Arrays

    Quote Originally Posted by lannie1980 View Post
    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
    It is very possible that given the same key, the last put wins.

Similar Threads

  1. Problem creating arrays
    By JohnPringle83 in forum New To Java
    Replies: 7
    Last Post: 05-06-2011, 11:01 PM
  2. Creating generic arrays
    By stijn1989 in forum Advanced Java
    Replies: 6
    Last Post: 11-18-2009, 01:26 PM
  3. Creating a constructor with arrays and arguments - Part 2
    By fullmetaljacket in forum New To Java
    Replies: 15
    Last Post: 07-05-2009, 01:38 PM
  4. Creating a constructor with arrays and arguments
    By fullmetaljacket in forum New To Java
    Replies: 38
    Last Post: 07-03-2009, 06:09 AM
  5. [SOLVED] Creating an Array of Arrays?
    By xcallmejudasx in forum Advanced Java
    Replies: 5
    Last Post: 11-04-2008, 06:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •