java - 由于相同的 HaschCode(重复对象),ArrayList 对象值更改

标签 java android arrays arraylist

我正在创建一个对象数组列表并随机将这些对象添加到其中:

public ArrayList makePerks(int roun, int ran) {
    perks.clear();
    int perkAm = Menu.randInt(6, 7);
    Perk last = new Perk();
    for (int i = 0; i < perkAm; ++i) {
        Perk tempPerk = new Perk();
        tempPerk = generate(ran, roun);
        System.out.println(tempPerk.name + " - " + tempPerk.cost);
        perks.add(tempPerk);
    }
    System.out.println("_________________________");
    return perks;
}

它的作用是使用其他一些函数随机创建津贴。此处的Print语句输出如下:

12-11 01:51:07.331: I/System.out(3900): Gain Profit - 4
12-11 01:51:07.341: I/System.out(3900): Strength - 11
12-11 01:51:07.351: I/System.out(3900): Gain Profit - 5
12-11 01:51:07.361: I/System.out(3900): High Premium - 17
12-11 01:51:07.371: I/System.out(3900): Moderate Speed - 5
12-11 01:51:07.381: I/System.out(3900): Nomad - 11
12-11 01:51:07.381: I/System.out(3900): _________________________

现在在我的一个 Activity (android) 中,我检索从上面的函数返回的数组列表:

tempItems = perkGen.makePerks(plrRound, plrRank);

现在我想像这样使用 for 循环将数据添加到 ArrayList 适配器:

        for (int i = 0; i < tempItems.size(); ++i) {
        shopItems.add(tempItems.get(i));
        System.out.println(tempItems.get(i).name + " - " + tempItems.get(i).cost);
        perk_adapter.notifyDataSetChanged();
    }

一切正常,除了输出如下:

12-11 01:51:07.381: I/System.out(3900): Gain Profit - 5
12-11 01:51:07.391: I/System.out(3900): Strength - 11
12-11 01:51:07.391: I/System.out(3900): Gain Profit - 5
12-11 01:51:07.391: I/System.out(3900): High Premium - 17
12-11 01:51:07.391: I/System.out(3900): Moderate Speed - 5
12-11 01:51:07.391: I/System.out(3900): Nomad - 11

如果你没有注意到,我有 2 个副本; (Gain Profit) 在第一个输出中,它们是 4 和 5,但在第二个输出中,它们是 5 和 5。这怎么可能?这与数组中有 2 个相似的对象有关吗?还是我做错了什么?

以下是带有哈希码的相同输出:

12-11 02:25:31.281: I/System.out(4024): Moderate Speed - 8 - -1307644808
12-11 02:25:31.291: I/System.out(4024): Agility - 9 - -1307646728
12-11 02:25:31.301: I/System.out(4024): Agility - 5 - -1307647072
12-11 02:25:31.301: I/System.out(4024): Prolix - 13 - -1307638016
12-11 02:25:31.321: I/System.out(4024): IDA - 5 - -1307641072
12-11 02:25:31.321: I/System.out(4024): Moderate Speed - 11 - -1307644808
12-11 02:25:31.321: I/System.out(4024): _________________________
12-11 02:25:31.321: I/System.out(4024): Moderate Speed - 11 - -1307644808
12-11 02:25:31.321: I/System.out(4024): Agility - 9 - -1307646728
12-11 02:25:31.321: I/System.out(4024): Agility - 5 - -1307647072
12-11 02:25:31.321: I/System.out(4024): Prolix - 13 - -1307638016
12-11 02:25:31.321: I/System.out(4024): IDA - 5 - -1307641072
12-11 02:25:31.331: I/System.out(4024): Moderate Speed - 11 - -1307644808

您可能还会注意到,在上面的输出中,敏捷保持正确的输出,而中速则不然。这是因为敏捷项目的哈希码相同。如何避免或改变这种情况?

谢谢你的帮助:)

如果我需要发布更多代码,请询问 :) 但我希望您拥有所需的所有信息 :) 再次感谢:)

根据要求对代码的另一补充生成方法:

public Perk generate(int ran, int roun) {
    final DecimalFormat mb = new DecimalFormat("0.00");
    int perkT = Menu.randInt(0, 4);
    Perk tempPerk = new Perk();

    if (perkT == 0) { // Speed
        ArrayList tempArray = new ArrayList();
        tempArray = getSpeedPerks(ran, roun);
        Perk tePerk = new Perk();
        tePerk = getPerk(tempArray);
        tempPerk = tePerk;
        int randBonus = Menu.randInt(0, 3);
        tempPerk.cost += randBonus;
        // Perk Increase Formula:
        double lvlBonus = roun * ran;
        double lvlInc = lvlBonus / 10;
        double bon = 1.0 + lvlInc;
        tempPerk.plusInc *= (bon);
        tempPerk.cost *= (bon);
        tempPerk.desc = ("Increases the effectiveness of each tap:\n"
                + "    - Tap Power + " + mb.format(tempPerk.plusInc));

    }
    if (perkT == 1) { // Minus
        ArrayList tempArray = new ArrayList();
        tempArray = getMinusPerks(ran, roun);
        Perk tePerk = new Perk();
        tePerk = getPerk(tempArray);
        tempPerk = tePerk;
        int randBonus = Menu.randInt(0, 3);
        tempPerk.cost += randBonus;
        // Perk Increase Formula:
        double lvlBonus = roun * ran;
        double lvlInc = lvlBonus / 10;
        double bon = 1.0 + lvlInc;
        tempPerk.minusDec *= (bon);
        tempPerk.cost *= (bon);
        tempPerk.desc = ("Decreases the bars speed:\n"
                + "   - Bar Speed - " + mb.format(tempPerk.minusDec));
    }
    if (perkT == 2) { // Auto
        ArrayList tempArray = new ArrayList();
        tempArray = getAutoPerks(ran, roun);
        Perk tePerk = new Perk();
        tePerk = getPerk(tempArray);
        tempPerk = tePerk;
        int randBonus = Menu.randInt(0, 3);
        tempPerk.cost += randBonus;
        // Perk Increase Formula:
        if (roun > 0 & roun <= 5) {
            int rand = Menu.randInt(0, 1);
            if (rand == 0) {
                tempPerk.autoClick = 30;
                tempPerk.clickAmount = 1;
            } else {
                tempPerk.autoClick = 60;
                tempPerk.clickAmount = 2;
            }
        } else if (roun > 5 & roun <= 10) {
            int rand = Menu.randInt(0, 1);
            if (rand == 0) {
                tempPerk.autoClick = 20;
                tempPerk.clickAmount = 1;
            } else {
                tempPerk.autoClick = 60;
                tempPerk.clickAmount = 3;
            }
        } else if (roun > 10 & roun <= 15) {
            int rand = Menu.randInt(0, 1);
            if (rand == 0) {
                tempPerk.autoClick = 15;
                tempPerk.clickAmount = 1;
            } else {
                tempPerk.autoClick = 20;
                tempPerk.clickAmount = 2;
            }
        } else if (roun > 15 & roun <= 20) {
            int rand = Menu.randInt(0, 1);
            if (rand == 0) {
                tempPerk.autoClick = 10;
                tempPerk.clickAmount = 1;
            } else {
                tempPerk.autoClick = 20;
                tempPerk.clickAmount = 2;
            }
        } else if (roun > 20 & roun <= 24) {
            int rand = Menu.randInt(0, 1);
            if (rand == 0) {
                tempPerk.autoClick = 5;
                tempPerk.clickAmount = 1;
            } else {
                tempPerk.autoClick = 10;
                tempPerk.clickAmount = 2;
            }
        } else if (roun == 25) {
            int rand = Menu.randInt(0, 1);
            if (rand == 0) {
                tempPerk.autoClick = 5;
                tempPerk.clickAmount = 2;
            } else {
                tempPerk.autoClick = 3;
                tempPerk.clickAmount = 1;
            }
        } else if (ran == 2) {
            int rand = Menu.randInt(0, 2);
            if (rand == 0) {
                tempPerk.autoClick = 5;
                tempPerk.clickAmount = 2;
            }
            if (rand == 1) {
                tempPerk.autoClick = 3;
                tempPerk.clickAmount = 1;
            }
            if (rand == 2) {
                tempPerk.autoClick = 2;
                tempPerk.clickAmount = 1;
            }
        } else if (ran == 3) {
            tempPerk.autoClick = 1;
            tempPerk.clickAmount = 1;
        } else {
            tempPerk.autoClick = 1;
            tempPerk.clickAmount = 1;
        }
        tempPerk.desc = ("Auto Taps at a fixed rate:\n" + "   - Taps + "
                + mb.format(tempPerk.clickAmount) + "\n   - every "
                + tempPerk.autoClick + " seconds");
    }
    if (perkT == 3) { // Reward
        ArrayList tempArray = new ArrayList();
        tempArray = getRewardPerks(ran, roun);
        Perk tePerk = new Perk();
        tePerk = getPerk(tempArray);
        tempPerk = tePerk;
        int randBonus = Menu.randInt(0, 3);
        tempPerk.cost += randBonus;
        // Perk Increase Formula:
        double lvlBonus = roun * ran;
        double lvlInc = lvlBonus / 10;
        double bon = 1.0 + lvlInc;
        tempPerk.cost *= (bon);
        tempPerk.rewardBonus += lvlInc / 5;
        tempPerk.desc = ("Increases amount of tokens gathered at the end of each round:\n"
                + "   - Tokens + " + tempPerk.rewardBonus);
    }
    if (perkT == 4) { // All Rounder
        ArrayList tempArray = new ArrayList();
        tempArray = getAllRounPerks(ran, roun);
        Perk tePerk = new Perk();
        tePerk = getPerk(tempArray);
        tempPerk = tePerk;
        int randBonus = Menu.randInt(0, 3);
        tempPerk.cost += randBonus;
        // Perk Increase Formula:
        double lvlBonus = roun * ran;
        double lvlInc = lvlBonus / 10;
        double bon = 1.0 + lvlInc;
        tempPerk.plusInc *= (bon);
        tempPerk.minusDec *= (bon);
        tempPerk.cost *= (bon);
        tempPerk.desc = ("Increases the effectiveness of several stats:\n"
                + "   - Tap Power + " + mb.format(tempPerk.plusInc)
                + "\n   - Bar Speed - " + mb.format(tempPerk.minusDec)
                + "\n   - Tokens + " + tempPerk.rewardBonus);
    }
    return tempPerk;
}

这里是这一行:

tePerk = getPerk(tempArray);

使 tePerk 等于列表中的额外津贴,这可能会导致问题。

再次感谢您:)

最佳答案

你能发布生成方法的代码吗?
我不确定,但从你提供的代码来看,看起来你在生成方法中重用了对象,例如:第一次以名称“Moderate Speed”返回并花费 8 的额外津贴,你将其添加到列表中甚至打印出来。然后再次将相同的对象用于值为 11 的“中等速度”,并将其添加到列表并打印它...现在列表有 2 个相同的额外对象。

自从您第一次添加和打印一项津贴后,您看到了 2 个不同的值。第二次在 for 循环中打印所有值,因此您看到的是相同的值。

关于java - 由于相同的 HaschCode(重复对象),ArrayList 对象值更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20512588/

相关文章:

ios - 在 Swift 3 中匹配和删除数组项

java - 在 REST API 中使用受检异常与未受检异常

java - 在 Java 类方法中嵌入 JavaScript 片段

android - 如何在android中顺时针和逆时针旋转图像(指尖旋转)?

android - 不同密度的不同 9-patch 图像?

javascript - 在纯javascript中选择任何随机div后,如何通过每次点击在循环中一个接一个地选择nextElementSibling项目?

glassfish 中的 Java 堆空间错误

java - 如何在 android 中的 contentProvider 类中获取 mainactivity.this

android - 如何获取 DialogFragment 附带的对话框? [安卓]

arrays - Excel VBA 数组 - 获取#Value!尝试将数组转储到电子表格时出错