IAmTheBee IAmTheBee - 1 year ago 95
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

variable is pointing to or referencing my
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
variable is supposed to let me start with a fresh ArrayList, but it also clears all information that is supposed to be in

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"))

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

for (String i: dice) {
num.add(Integer.parseInt(i.substring(0, 1)));
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());


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

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)
if (list.get(i) == high)
if (list.get(i) >= 10) {
temp = rand.nextInt(type.get(i)) + 1;
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.


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 Source
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download