简体   繁体   English

存储在数组中的数组将被覆盖为最后一个添加的内容

[英]Array stored in array gets overwritten to the contents of the last one added

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: 这是投掷3次3次骰子的示例输出:

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. 您正在将相同的数组对象dieThrown添加到多次thrown的数组中。 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. 通过d for循环的每次迭代,您都将覆盖同一数组中的值,因此最后一次迭代的值就是剩余的值。 The thrown array is full of references to the same array object referred to by dieThrown . thrown数组充满了对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: 您可以通过移动dieThrown数组的声明来做到这一点:

int[] dieThrown = new int[D];

to the first line of the body of the t for loop, before the d for loop. d for循环之前的t for循环主体的第一行。

dieThrown is the same object throughout the program. dieThrown是整个程序中的同一对象。 You fill up your thrown array with references to the same object, and just replace its content in each iteration. 您可以使用对同一对象的引用来填充thrown数组,并在每次迭代中替换其内容。 So all your thrown entries basically point to the same place, and show the last content that has been assigned to dieThrown . 因此,所有thrown条目基本上都指向同一位置,并显示已分配给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 . 您也可以摆脱dieThrown You can directly add it to thrown . 您可以将其直接添加到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 请注意,我加入到thrown直接

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. 您将重复使用dieThrown每个循环,而无需在每次迭代中创建新的数组。 You can solve this by moving the initialisation of dieThrown to the start of the outer loop (see it running here: IDEONE ); 您可以通过将dieThrown的初始化移动到外循环的开头来解决此问题(请参见此处运行的IDEONE ); or, a better solution, is to get rid of dieThrown and update the thrown array directly. 或者,更好的解决方案是摆脱dieThrown并直接更新thrown数组。

You can also simplify everything using java.util.Random#nextInt(int) instead of Math.random . 您还可以使用java.util.Random#nextInt(int)代替Math.random简化一切。

IDEONE 爱迪生

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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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