<分区>
我在使用 ArrayLists
在我的 java 类中实现 HashMaps
时遇到了问题。问题是它不断向 ArrayList
添加对象是 HashMap
,即使我没有更新我的 HashMap
。
这是我无法理解的代码:
HashMap<String, ArrayList<String>> map = new HashMap<>();
ArrayList<String> array = new ArrayList<String>();
array.add("One");
array.add("Two");
map.put("Key", array);
array.add("Three"); //2. Why does this get added to the HashMap?
System.out.println(map1.get("Key"));
//1. This print out [One, Two, Three].. When it should be [One, Two]!