为什么我的嵌套哈希图将所有值设置为相同的值?
这是我第一次涉足 Java,我很难理解地图的工作原理。我有一个 for 循环,它将信息映射附加到另一个映射中。然而,当我查看最外面的地图时,它包含正确数量的子地图,但是它们都包含相同的信息,即使我最初在其中放入了不同的信息......有人知道为什么吗?
谢谢,
Lemiat
下面我包含了我的代码和 System.out.print 输出:
代码:
Map continents = new HashMap();
Map continentData = new HashMap();
int lastContinent = -1;
//Accumulate Continent data
for(Country currCountry : countries){
continentData.clear();
int currContinent = currCountry.getContinent();
continentData.put("P", 0);
continentData.put("E", 1);
continentData.put("O", 2);
continentData.put("Bonus",currContinent);
if(currContinent != lastContinent){
continents.put(currContinent, continentData);
System.out.println("add");
System.out.println(continentData);
}
lastContinent = currContinent;
}
System.out.println(continents.toString());
结果:
add
{E=1, P=0, O=2, Bonus=0}
add
{E=1, P=0, O=2, Bonus=1}
add
{E=1, P=0, O=2, Bonus=2}
add
{E=1, P=0, O=2, Bonus=3}
add
{E=1, P=0, O=2, Bonus=4}
add
{E=1, P=0, O=2, Bonus=5}
{0={E=1, P=0, O=2, Bonus=5}, 1={E=1, P=0, O=2, Bonus=5}, 2={E=1, P=0, O=2, Bonus=5}, 3={E=1, P=0, O=2, Bonus=5}, 4={E=1, P=0, O=2, Bonus=5}, 5={E=1, P=0, O=2, Bonus=5}}
This is my first foray into java, and I am having trouble understanding how maps work. I have a for loop which appends maps of information into another map. However when I look at the outermost map it contains the correct number of sub-maps, however they all conatin the same information, even though I originally put different information in them... does anyone know why?
Thanks,
Lemiant
Below I have included my code and the System.out.print output:
Code:
Map continents = new HashMap();
Map continentData = new HashMap();
int lastContinent = -1;
//Accumulate Continent data
for(Country currCountry : countries){
continentData.clear();
int currContinent = currCountry.getContinent();
continentData.put("P", 0);
continentData.put("E", 1);
continentData.put("O", 2);
continentData.put("Bonus",currContinent);
if(currContinent != lastContinent){
continents.put(currContinent, continentData);
System.out.println("add");
System.out.println(continentData);
}
lastContinent = currContinent;
}
System.out.println(continents.toString());
Result:
add
{E=1, P=0, O=2, Bonus=0}
add
{E=1, P=0, O=2, Bonus=1}
add
{E=1, P=0, O=2, Bonus=2}
add
{E=1, P=0, O=2, Bonus=3}
add
{E=1, P=0, O=2, Bonus=4}
add
{E=1, P=0, O=2, Bonus=5}
{0={E=1, P=0, O=2, Bonus=5}, 1={E=1, P=0, O=2, Bonus=5}, 2={E=1, P=0, O=2, Bonus=5}, 3={E=1, P=0, O=2, Bonus=5}, 4={E=1, P=0, O=2, Bonus=5}, 5={E=1, P=0, O=2, Bonus=5}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将
Map ContinentalData = new HashMap();
移至for
循环内,以便在每次传递时创建单独的地图。现在你只是一遍又一遍地更新同一张地图。You need to move
Map continentData = new HashMap();
inside yourfor
loop so that a separate map is created on each pass. Right now you're just updating the same map over and over.您对所有地图使用相同的
ContinentalData
实例。您必须在每次循环交互中为其实例化一个新的Map
。You are using the same instance of
continentData
for all the maps. You must instantiate a newMap
for it in each loop interation.