Note that there are some explanatory texts on larger screens.

plurals
  1. POArrayList Object Value Change due to same HaschCode (Duplicate Objects)
    primarykey
    data
    text
    <p>Im creating an array List of objects and adding those objects to it randomly:</p> <pre><code>public ArrayList makePerks(int roun, int ran) { perks.clear(); int perkAm = Menu.randInt(6, 7); Perk last = new Perk(); for (int i = 0; i &lt; 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; } </code></pre> <p>What this does, is it creates perks randomly using some other functions. The output from the Print statement in this is as follows:</p> <pre><code>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): _________________________ </code></pre> <p>Now inside one of my activities (android), i retrieve the array List that is returned from the function above:</p> <pre><code>tempItems = perkGen.makePerks(plrRound, plrRank); </code></pre> <p>And now i want to add the data to an ArrayList adapter using a for loop like so:</p> <pre><code> for (int i = 0; i &lt; tempItems.size(); ++i) { shopItems.add(tempItems.get(i)); System.out.println(tempItems.get(i).name + " - " + tempItems.get(i).cost); perk_adapter.notifyDataSetChanged(); } </code></pre> <p>It all works fine except the output is as follows:</p> <pre><code>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 </code></pre> <p>If you havent noticed, where i have 2 duplicates; (Gain Profit) in the first output, they are 4 and 5, but in the second one they are 5 and 5. How is this possible? Does this have something to do with there being 2 similar objects in the array? Or is it something im doing wrong ?</p> <p>Here are the same outputs with hashcodes:</p> <pre><code>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 </code></pre> <p>You may also notice that in the output above, the agility maintains the correct output, the moderate Speed however does not. This is because the hashcodes are the same for the agility items. How can this be avoided or changed?</p> <p>Thanks you for your help :) </p> <p>If i need to post more code just ask :) But i hope you have all the information you need :) Thanks again :)</p> <p>Another Addition to the code as requested THE GENERATE METHOD:</p> <pre><code>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 &gt; 0 &amp; roun &lt;= 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 &gt; 5 &amp; roun &lt;= 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 &gt; 10 &amp; roun &lt;= 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 &gt; 15 &amp; roun &lt;= 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 &gt; 20 &amp; roun &lt;= 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; } </code></pre> <p>This line here:</p> <pre><code>tePerk = getPerk(tempArray); </code></pre> <p>Makes the tePerk equal to a perk from a list, this may be causing the problem.</p> <p>Thank you again :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload