Maybe it's something simple I'm missing, but I can't seem to figure out why my array of arrays is being overwritten with the last value in that is put into the array.
This is my code:
import java.util.Arrays;
import class.ArgsProcessor;
public class Dice {
public static void main(String[] args) {
ArgsProcessor ap = new ArgsProcessor(args);
int D = ap.nextInt("How many dice will be thrown each time?");
int T = ap.nextInt("How many throws?");
int[] dieThrown = new int[D];
int [][] thrown = new int[T][D];
double randomNum;
for (int t = 0; t < T; t++){
for (int d = 0; d < D; d++){
randomNum = Math.random();
if (randomNum < 1.0/6.0)
dieThrown[d] = 1;
else if (randomNum < 2.0/6.0)
dieThrown[d] = 2;
else if (randomNum < 3.0/6.0)
dieThrown[d] = 3;
else if (randomNum < 4.0/6.0)
dieThrown[d] = 4;
else if (randomNum < 5.0/6.0)
dieThrown[d] = 5;
else
dieThrown[d] = 6;
}
System.out.println("On throw " + (t+1) + " we rolled:");
System.out.println("\t" + Arrays.toString(dieThrown));
thrown[t] = dieThrown;
}
System.out.println(Arrays.deepToString(thrown));
}
}
Here's an example output for 3 dice thrown 3 times:
On throw 1 we rolled:
[3, 4, 2]
On throw 2 we rolled:
[1, 5, 4]
On throw 3 we rolled:
[6, 5, 3]
[[6, 5, 3], [6, 5, 3], [6, 5, 3]]
However, I'm expecting something like this:
On throw 1 we rolled:
[3, 4, 2]
On throw 2 we rolled:
[1, 5, 4]
On throw 3 we rolled:
[6, 5, 3]
[[3, 4, 2], [1, 5, 4], [6, 5, 3]]
How can I get it to add the right values?
You are adding the same array object dieThrown
to the array thrown
multiple times. Each iteration through the d
for loop, you are overwriting the values in the same array, so the last iteration's values are the ones that remain. The thrown
array is full of references to the same array object referred to by dieThrown
.
Create a new array in each iteration, separate from any previous iteration's array. You can do this by moving the declaration of the dieThrown
array:
int[] dieThrown = new int[D];
to the first line of the body of the t
for loop, before the d
for loop.
dieThrown
is the same object throughout the program. You fill up your thrown
array with references to the same object, and just replace its content in each iteration. So all your thrown
entries basically point to the same place, and show the last content that has been assigned to dieThrown
. To avoid that, use
dieThrown = new int[D]:
inside your outer loop, so that each element will be a separate array that will keep the content.
You could also get rid of dieThrown
. You can directly add it to thrown
. Following is the code
public class dice {
public static void main(String[] args) {
int D = 3;
int T = 3;
int [][] thrown = new int[T][D];
double randomNum;
for (int t = 0; t < T; t++){
for (int d = 0; d < D; d++){
randomNum = Math.random();
if (randomNum < 1.0/6.0)
thrown[t][d] = 1;
else if (randomNum < 2.0/6.0)
thrown[t][d] = 2;
else if (randomNum < 3.0/6.0)
thrown[t][d] = 3;
else if (randomNum < 4.0/6.0)
thrown[t][d] = 4;
else if (randomNum < 5.0/6.0)
thrown[t][d] = 5;
else
thrown[t][d] = 6;
}
System.out.println("On throw " + (t+1) + " we rolled:");
System.out.println("\t" + Arrays.toString(thrown[t]));
//thrown[t] = dieThrown;
}
System.out.println(Arrays.deepToString(thrown));
}
}
Notice I am adding to thrown
directly
Output
On throw 1 we rolled:
[5, 4, 4]
On throw 2 we rolled:
[4, 4, 2]
On throw 3 we rolled:
[6, 6, 6]
[[5, 4, 4], [4, 4, 2], [6, 6, 6]]
You are re-using dieThrown
each loop without creating a new array each iteration. You can solve this by moving the initialisation of dieThrown
to the start of the outer loop (see it running here: IDEONE ); or, a better solution, is to get rid of dieThrown
and update the thrown
array directly.
You can also simplify everything using java.util.Random#nextInt(int)
instead of Math.random
.
public static void main( String[] args ){
int D = 3;
int T = 4;
int [][] thrown = new int[T][D];
Random rand = new Random();
for (int t = 0; t < T; t++){
for (int d = 0; d < D; d++){
thrown[t][d] = rand.nextInt(6)+1;
}
System.out.println("On throw " + (t+1) + " we rolled:");
System.out.println("\t" + Arrays.toString(thrown[t]));
}
System.out.println(Arrays.deepToString(thrown));
}
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.