import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.Map;
public class Reminder {
public static void main(String args[]) {
ArrayList<String> arrStr = new ArrayList<>(); Map<String,ArrayList<String>> mm = new HashMap<String, ArrayList<String>>();
arrStr.add("12");
arrStr.add("23");
mm.put("1",arrStr); Iterator<Map.Entry<String, ArrayList<String>>> at = mm.entrySet().iterator(); while (at.hasNext()){ Map.Entry<String, ArrayList<String>> entry=at.next();
System.err.println(entry.getValue());
}
arrStr.set(1,"222"); Iterator<Map.Entry<String, ArrayList<String>>> at2 = mm.entrySet().iterator(); while (at2.hasNext()){ Map.Entry<String, ArrayList<String>> entry=at2.next();
System.err.println(entry.getValue());
}
}
}程序输出:如上代码,当arrStr改变的时候,mm也跟着改变了。怎么才能让arrStr更改的同时,mm不变呢?
2 回答
德玛西亚99
TA贡献1770条经验 获得超3个赞
HashMap<Integer, Integer> map = new HashMap<>();
HashMap<Integer, Integer> map2 = new HashMap<>(map);
添加回答
举报
0/150
提交
取消