IAmTheBee IAmTheBee - 3 months ago 11
Java Question

How do I make an ArrayList not a pointer

So, I'm writing this program simply to simulate dice rolls for probability reasons. And I've found the problem, I know what it's doing, I just don't know how to fix it.

The main issue (besides poor coding) is that my

succ
variable is pointing to or referencing my
points
variable. Therefore when it's time to get averages, I get averages for the culmination of all the sets of dicests instead of an average of each diceset. Clearing the
points
variable is supposed to let me start with a fresh ArrayList, but it also clears all information that is supposed to be in
succ


To anyone interested in running this program, enter in dicesets like this: 3d6. It was a personal choice, I could make it account for any number of n-sided dice but for my purposes I only need 5 max.

import java.util.Random;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;

public class DiceThing {
static Scanner scan = new Scanner(System.in);
static int trials;
static String bleh = new String("hurr");
static ArrayList < String > dice = new ArrayList < String > ();
static ArrayList < Integer > roll = new ArrayList < Integer > ();
static ArrayList < Integer > points = new ArrayList < Integer > ();
static ArrayList < Integer > num = new ArrayList < Integer > ();
static ArrayList < Integer > type = new ArrayList < Integer > ();
static ArrayList < ArrayList < Integer >> succ = new ArrayList < ArrayList < Integer >> ();

static Random rand = new Random();

public static void main(String[] args) {
System.out.println("Enter any number of dicesets. Enter 0 to stop");
while (!bleh.equals("0")) {
System.out.println("Enter diceset: ");
bleh = scan.nextLine();
if (!bleh.equals("0"))
dice.add(bleh);
}

System.out.println("Enter number of trials: ");
trials = scan.nextInt();

for (String i: dice) {
num.add(Integer.parseInt(i.substring(0, 1)));
type.add(Integer.parseInt(i.substring(2)));
}
for (int i = 0; i < dice.size(); i++) { //count place in dicesets
for (int j = 0; j < trials; j++) { //count trials
for (int k = 0; k < num.get(i); k++) //count number of dice in the diceset
roll.add(rand.nextInt(type.get(i)) + 1);

points.add(countSuccess(roll, type.get(i)));
System.out.println(roll.toString() + points.toString());

roll.clear();
}
succ.add(points);
points.clear();
System.out.println(succ.toString());
}

System.out.println("Average success of dice sets:");
for (ArrayList < Integer > i: succ) {
System.out.println(average(i));
}
}

public static int countSuccess(ArrayList < Integer > list, int high) {
int suc = 0, temp;
for (int i = 0; i < list.size(); i++) {
if (list.get(i) >= 5)
suc++;
if (list.get(i) == high)
suc++;
if (list.get(i) >= 10) {
temp = rand.nextInt(type.get(i)) + 1;
roll.add(temp);
list.add(temp);
}
}
return suc;

}

private static double average(ArrayList < Integer > marks) {
Integer sum = 0;
if (!marks.isEmpty()) {
for (Integer mark: marks) {
sum += mark;
}
return sum.doubleValue() / marks.size();
}
return sum;
}
}


The issue is occurring here though the origin may be elsewhere.

succ.add(points);
points.clear();
System.out.println(succ.toString());


I probably didn't explain very well and if that's the case then let me know. Any help and explanation would be appreciated.

Answer
succ.add(new ArrayList(points));

If I understood you correctly, you want to separate a list in your list from the origin one, don't you?

If so then just make a new list and copy values there from the origin collection. A constructor ArrayList(Collection<? extends E> c) will help.

After that, the points.clear() call won't change a value in the list.
Only succ.get(index).clear() will clear it.

Comments