I want to remove values from a copy of an ArrayList without affecting the original copy. Code currently:
statsOf2Pairs runs first then statsOfFullHouse, but the original list is has its values removed when I remove them from a copy in statsOf2Pairs.
public void statsOf2Pairs(List<List<Integer>> allRolls) {
long count = 0;
long debug = 0;
List<List<Integer>> rolls = new ArrayList<List<Integer>>();
rolls = allRolls;
for (List<Integer> roll : rolls) {
if(roll.size() < 5) {
debug++;
}
if (hasMultiplesOf(roll, 2)) {
roll.removeAll(Arrays.asList(mRepeatedDice));
if (hasMultiplesOf(roll, 2)) {
count++;
}
}
}
System.out.println("The amount of 2 pairs in all the rolls possible: "
+ count);
System.out.println("So the chance of rolling 2 pairs is: "
+ ((double) count / (double) mTotalPossibleRolls) * 100d + " %");
}
public void statsOfFullHouse(List<List<Integer>> allRolls) {
long count = 0;
long debug = 0;
for (List<Integer> roll : allRolls) {
if(roll.size() < 3) {
debug++;
}
if (hasMultiplesOf(roll, 3)) {
roll.removeAll(Arrays.asList(mRepeatedDice));
if (hasMultiplesOf(roll, 2)) {
count++;
}
}
}
System.out.println("The amount of Full Houses in all the rolls possible: "
+ count);
System.out.println("So the chance of rolling a Full House is: "
+ ((double) count / (double) mTotalPossibleRolls) * 100d + " %");
}
I assume you are wanting to copy the allRolls array instead of assign it to rolls.
Instead of
Try
This will create a new list (shallow copy) of the allRolls list. The original example was assigning the allRolls to rolls, not copying it.