0

I would like to store HashMaps in an array. I'm trying to make just one HashMap, fill it with specific information and store it into one element of an array. Then I would like to overwrite the information in that Hashmap with different information and then store that into a different element of that array. I would like to do this multiple times. What's the best way to go about doing this?

Right now I have:

HashMap[][] location = new HashMap[columns][rows];
HashMap <String, String> area = new HashMap <String, String> ();

public Map() {

    area.put("description", "You are in the upper left\n");
    location[0][0] = area;

    area.put("description", "You are in the upper middle\n");
    location[1][0] = area;
}

The problem with this is that now both location[0][0] and location[1][0] have the same description.

5
  • You should use Map for area instead of HashMap. It's a good practice in the sense that it'll be easier to change implementation in future. Commented Oct 18, 2012 at 18:45
  • I'm not sure if my area and your area are the same. I'm using area in terms of a location on a map, not mathematically. Is using a HashMap better in my case? Commented Oct 18, 2012 at 19:35
  • You are confusing the literal meaning of Map with the Map interface of Java. I meant you should initialize your area variable as, Map<String, String> area = new HashMap <String, String>(); This would help you in future when you want to shift to let say LinkedHashMap, Treemap or any other flavor of Map. Commented Oct 18, 2012 at 19:42
  • I didn't think of that. In fact, I didn't know those other ones even existed. Thanks. Commented Oct 18, 2012 at 20:18
  • Then I suggest you take a look at them and figure out the benefactions over one another. It's always good to have a reason before choosing a data structure. :) Commented Oct 18, 2012 at 20:23

2 Answers 2

2

location[0][0] and location[1][0] hold the same pointer to area

you should do like this

location[0][0] = createArea("You are in the upper left\n");
location[1][0] = createArea("You are in the upper middle\n");



HashMap <String, String> createArea(String desc){
    HashMap <String, String> area = new HashMap <String, String> ();
    area.put("description", desc);
    return area;
}
Sign up to request clarification or add additional context in comments.

1 Comment

That did it! Thanks. I like how simple it is. I knew I needed to make a new HashMap but I couldn't picture what the code would look like. Methods are key.
0

You need to create a different instance of the Map to be stored in each location.

 public Map() {      

   Map<String, String> area = new HashMap<String, String>();
   area.put("description", "You are in the upper left\n");     
   location[0][0] = area;      

   area = new HashMap<String, String>();
   area.put("description", "You are in the upper middle\n");    
   location[1][0] = area; } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.