我想将 HashMap 存储在数组中。我正在尝试制作一个 HashMap,用特定信息填充它并将其存储到数组的一个元素中。然后我想用不同的信息覆盖该 HashMap 中的信息,然后将其存储到该数组的不同元素中。我想多次这样做。执行此操作的最佳方法是什么?
现在我有:
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;
}
问题是现在 location[0][0] 和 location[1][0] 具有相同的描述。
最佳答案
location[0][0]和location[1][0]持有相同的区域指针
你应该这样做
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;
}
关于java - 将 hashMap 存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12961095/