【发布时间】:2013-12-29 00:56:47
【问题描述】:
我正在创建一个对象数组列表并将这些对象随机添加到其中:
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): _________________________
现在在我的一个活动 (android) 中,我检索从上述函数返回的数组 List:
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 等于列表中的 perk,这可能会导致问题。
再次感谢您:)
【问题讨论】:
-
嗨,为什么你使用了代码` Perk last = new Perk(); ` 你打算在任何地方访问 perks 实例变量吗?
-
Perk tempPerk = new Perk();这条线没用,因为你正在用这条线tempPerk = generate(ran, roun);更改 tempPerk -
嘿。我目前没有使用 last 它与当前代码无关。但我稍后会使用它。是的 tempPerk 是在 generate() 函数中生成的,如您所见: tempPerk = generate()
-
尽管它在我第一次打印出来时给了我正确的价值,所以它不应该与我如何正确生成它有关?
-
你能看到打印
tempItems.get(i).hashcode()吗
标签: java android arrays arraylist