【发布时间】:2018-08-31 16:57:12
【问题描述】:
我完全不知所措。这是我的代码
HashMap>> themap = new HashMap>>(); HashMap> outerMap = new HashMap>();
...
//themap is populated through OnCreate(), and works fine.
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
String currentfragment = "my_string"
...
HashMap<String, HashMap<String, HashMap<String, Integer>>> copymap = copy(themap);
System.out.println("1" + themap);
outerMap.putAll(copymap.get(currentfragment));
System.out.println("2" + themap);
...
}
在另一种方法中,themap 的某些值被更改。问题是当 onNavigationItemSelected 被调用时,第一个.println() 打印正确的地图,改变了值,第二个.println() 打印了themap 的起始值,在它们被改变之前。我做错了什么?
编辑:我已按照How to copy HashMap (not shallow copy) in Java 的说明进行操作,但仍然无法正常工作。我已经更新了我之前的代码,这是我的copy 方法:
public static HashMap<String, HashMap<String, HashMap<String, Integer>>> copy(HashMap<String, HashMap<String, HashMap<String, Integer>>> original) {
HashMap<String, HashMap<String, HashMap<String, Integer>>> copy = new HashMap<>();
for (Map.Entry<String, HashMap<String, HashMap<String, Integer>>> entry : original.entrySet()) {
HashMap<String, HashMap<String, Integer>> copyInnerMap = new HashMap<>();
for (Map.Entry<String, HashMap<String, Integer>> innerEntry : entry.getValue().entrySet()) {
HashMap<String, Integer> innerInnerMap = innerEntry.getValue();
HashMap<String, Integer> copiedInnerInnerMap = new HashMap<>();
for (Map.Entry<String, Integer> innerInnerMapEntry : innerInnerMap.entrySet()) {
copiedInnerInnerMap.put(innerInnerMapEntry.getKey(), innerInnerMapEntry.getValue());
}
copyInnerMap.put(innerEntry.getKey(), copiedInnerInnerMap);
}
copy.put(entry.getKey(), copyInnerMap);
}
return copy;
}
EDIT2:我已经更新了我的复制方法,使其足够深。
这是第一个输出:
1{compu={Day1={Work hours=0, Study hours=0, Day grades=-1, Sheetsfilled=0, Fun=0}, Day2={Work hours=0, Study hours=0, Day grades= -1, 填满的表格=0, 乐趣=0}, 第 3 天={工作时间=0, 学习时间=0, 日成绩=-1, 填满的表格=0, 有趣=0}}, 化学={第 1 天={工作小时=0, 学习时间=0, 日成绩=-1, 填满表格=0, 乐趣=0}, 第2 天={工作时间=0, 学习时间=0, 日成绩=-1, 填满表格=0, 有趣=0}, Day3={工作时间=0, 学习时间=0, 一天成绩=-1, 填满的表格=0, 乐趣=0}}, 数学={Day1={工作时间=0, 学习时间=0,一天成绩=-1,填满表格=0,乐趣=0},Day2={工作时间=0,学习时间=0,一天成绩=-1,填满表格=0,乐趣=2}, Day3={Work hours=0, Study hours=0, Day grades=-1, Sheetsfilled=0, Fun=0}}, physic={Day1={Work hours=0, Study hours=0, Day grades =-1, 填满表格=0, 乐趣=0}, Day2={工作时间=0, 学习时间=0, 日成绩=-1, 填满表格=0, Fun=0}, Day3={工作时间=0 , 学习时间=0, 日成绩=-1, 填写的表格=0, 乐趣=0}}, 无={Day1={工作时间=0, 学习时间=0, 日成绩s=-1, 已填表=0, 乐趣=0}, 第 2 天={工作时间=0, 学习时间=0, 日成绩=-1, 已填表=0, 有趣=2}, 第 3 天={工作时间= 0, 学习时间=0, 日成绩=-1, 填满表格=0, 乐趣=0}}}
第二个:
2{compu={Day1={Work hours=0, Study hours=0, Day grades=-1, Sheetsfilled=0, Fun=0}, Day2={Work hours=0, Study hours=0, Day grades= -1, 填满的表格=0, 乐趣=0}, 第 3 天={工作时间=0, 学习时间=0, 日成绩=-1, 填满的表格=0, 有趣=0}}, 化学={第 1 天={工作小时=0, 学习时间=0, 日成绩=-1, 填满表格=0, 乐趣=0}, 第2 天={工作时间=0, 学习时间=0, 日成绩=-1, 填满表格=0, 有趣=0}, Day3={工作时间=0, 学习时间=0, 一天成绩=-1, 填满的表格=0, 乐趣=0}}, 数学={Day1={工作时间=0, 学习时间=0,一天成绩=-1,填满表格=0,乐趣=0},Day2={工作时间=0,学习时间=0,一天成绩=-1,填满表格=0,乐趣=0}, Day3={Work hours=0, Study hours=0, Day grades=-1, Sheetsfilled=0, Fun=0}}, physic={Day1={Work hours=0, Study hours=0, Day grades =-1,填满的表格=0,乐趣=0},第2天={工作时间=0,学习时间=0,日成绩=-1,填满的表格=0,有趣=0},第3天={工作时间=0 , 学习时间=0, 日成绩=-1, 填写的表格=0, 乐趣=0}}, 无={Day1={工作时间=0, 学习时间=0, 日成绩s=-1, 填满表格=0, 乐趣=0}, Day2={工作时间=0, 学习时间=0, 日成绩=-1, 填满表格=0, Fun=0}, Day3={工作时间= 0, 学习时间=0, 日成绩=-1, 填满表格=0, 乐趣=0}}}
【问题讨论】:
-
putAll&put不会将对象深度复制到新的outerMap,它只是将引用存储在Map中。 -
但是为什么
themap在我复制其内容的那一行之后发生了变化?我应该怎么做才能复制它的内容而不修改它? -
可能外部方法通过
outerMap改变了值 -
你能看看我的编辑吗?
-
您不应该编辑您的问题以输入答案。答案与问题是分开的。