为什么我的嵌套哈希图将所有值设置为相同的值?

发布于 2024-09-29 15:33:25 字数 1260 浏览 0 评论 0原文

这是我第一次涉足 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情释 2024-10-06 15:33:25

您需要将 Map ContinentalData = new HashMap(); 移至 for 循环内,以便在每次传递时创建单独的地图。现在你只是一遍又一遍地更新同一张地图。

You need to move Map continentData = new HashMap(); inside your for loop so that a separate map is created on each pass. Right now you're just updating the same map over and over.

七分※倦醒 2024-10-06 15:33:25

您对所有地图使用相同的 ContinentalData 实例。您必须在每次循环交互中为其实例化一个新的Map

for(Country currCountry : countries){
    //continentData.clear();
    Map continentData = new HashMap();
    int currContinent = currCountry.getContinent();

You are using the same instance of continentData for all the maps. You must instantiate a new Map for it in each loop interation.

for(Country currCountry : countries){
    //continentData.clear();
    Map continentData = new HashMap();
    int currContinent = currCountry.getContinent();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文