简体   繁体   中英

adding 1 to an ArrayList item

I'm checking the frequency of the dice totals after 36 million rolls. I planned to make an arrayList and use it as a tally system when any of the 11 results came up. How would I add 1 to one of the items in the list?

int die1, die2, dicetotal;

ArrayList<Integer> results = new ArrayList<Integer>();  
results.add(0); //for the 11th

for(int i = 0; i < 36000000; i++){
          die1 = (int)(Math.random() * 6) + 1
          die2 = (int)(Math.random() * 6) + 1
          dicetotal = die1 + die2;

          Switch (dicetotal){
                 case 2: results.set(0, CURRENT + 1);
                 ...
                 ...
                 ...

      }

ArrayList would be overkill for this. But if you must, (untested code)

First initalize your array to contain 13 elements (0 to 12) so that IndexOutOfBoundsException won't pop up. They would be initialized to zero.

results = new ArrayList<Integer>(13);

Then just get the element, add one, and set it

results.set(dicetotal, results.get(dicetotal) + 1);

Indeed, you should just use an int[] if you know the size of the array beforehand, and won't change during the program. They are faster than ArrayList .

// initialize
results = new int[13];
// could be new int[11]; if you put results in array elements 0 to 10.

// ...

// add to tally
results[dicetotal]++;

// or, instead, pack the array because dice roll results can only be in 2 to 12. 
// Put such results in array element 0 to 10, 
// by subtracting 2 to the index before putting in
results[dicetotal - 2]++;

It would make more sense to store the frequencies in an array of integers. For one, it would be much simpler to increment the value for a particular result:

int[] frequencies = new int[11]; // sum of two dice can be 2-12

for(int i = 0; i < 36000000; i++){
    die1 = (int)(Math.random() * 6) + 1
    die2 = (int)(Math.random() * 6) + 1
    dicetotal = die1 + die2;

    // increment frequency:
    frequencies[dicetotal-2] = frequencies[dicetotal-2]+1;
}

Now your frequencies array has the frequency of the dice result of two at index 0.

Use a fixed-size array for 11 possible scores you can get:

int die1, die2, dicetotal;

int[] totals= new int[11];
for(int i = 0; i < 36000000; i++){
    die1 = (int)(Math.random() * 6) + 1
    die2 = (int)(Math.random() * 6) + 1
    dicetotal = die1 + die2;

    //just increment the right position
    totals[dicetotal-2]++;
}

This may seem like an overkill but creating a wrapper for the counter so you can store it in a map will make the code a bit easier at the eys and easier to expand.

public static class Count {
    private int count = 0;

    public int getCount() {
        return count;
    }

    public void increment() {
        count++;
    }

    @Override
    public String toString() {
        return "" + count;
    }
}

public static void main(String[] args) {
    Map<Integer, Count> diceHistogram = new HashMap<>();
    for (int i = 2; i <= 12; i++) {
        diceHistogram.put(i, new Count());
    }

    for (int i = 0; i < (1E+6); i++) {
        int diceOne = rnd.nextInt(6) + 1;
        int diceTwo = rnd.nextInt(6) + 1;
        int sum = diceOne + diceTwo;

        diceHistogram.get(sum).increment();
    }

    System.out.println(diceHistogram);
}

Outputs the number of occurances for each dice-combo.

2=28043, 3=55745, 4=83489, 5=110517, 6=138823, 7=166928, 8=138466, 9=111321, 10=83532, 11=55469, 12=27667

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM