简体   繁体   English

'镜像'二维数组[Java]

[英]'Mirroring' a two-dimensional array [Java]

I am attempting to make "2-dimensional" array in Java that populates itself with random numbers between 1 and 6 (EDIT: I meant 0 and 5, as the code does currently - my apologies). 我试图在Java中制作“二维”数组,用1到6之间的随机数填充自己(编辑:我的意思是0和5,正如代码目前所做的那样 - 我的道歉)。 However, I would like the numbers to be 'mirrored' over an imaginary line-of-best-fit, as if dealing with a graph. 但是,我希望这些数字能够在假想的最佳拟合线上“镜像”,就像处理图形一样。 For example, if the number at indicies [1][4] is 3.0, I would like the number at [4][1] to also be 3.0. 例如,如果标记[1] [4]的数字是3.0,我希望[4] [1]的数字也是3.0。 The snippet of code I currently have that deals with this is below, for what its worth (I have array.length established as 6 at another point in the code): 我现在处理的代码片段如下所示:它的价值(我在代码中的另一个点将array.length建立为6):

Random random = new Random();

        int n = array.length;

        double [][] populationArray = new double [n][n];

        for (int i=0; i<populationArray.length; i++){
            for (int j=0; j<populationArray[i].length; j++) {
                populationArray[i][j] = random.nextInt(6);
                if (populationArray[i][j] != 0) {
                    populationArray[j][i] = populationArray[i][j];
                }
            }
        }

        for (double[] p : populationArray) {
            System.out.println(Arrays.toString(p));
        }

This currently prints as: 目前打印为:

[0.0, 1.0, 2.0, 1.0, 5.0, 2.0]
[1.0, 3.0, 5.0, 1.0, 3.0, 1.0]
[2.0, 5.0, 4.0, 1.0, 4.0, 1.0]
[1.0, 1.0, 1.0, 4.0, 2.0, 2.0]
[5.0, 3.0, 0.0, 2.0, 4.0, 4.0]
[2.0, 1.0, 0.0, 0.0, 4.0, 1.0]

As you can see some of the numbers reflect and some don't (I suspect the ones that do are purely out of luck), and I'm struggling with the logic of this. 你可以看到一些数字反映出来,有些则没有(我怀疑这些数字纯粹是运气不好),而我正在努力解决这个问题。 I would appreciate any suggestions - if this has been addressed somewhere else I'd also take a link, as I could not find it myself. 我会很感激任何建议 - 如果这已经在其他地方得到解决,我也会接受一个链接,因为我自己找不到它。

Thank you. 谢谢。

Because of the if(populationArray[i][j] != 0) only the values that aren't 0 are mirrored. 由于if(populationArray[i][j] != 0)只镜像非0的值。 if you remove the if statment the code will work. 如果删除if语句,代码将起作用。

Also random.nextInt(6) will generate an integer between 0 (inclusive) and 6 (exclusive), so that will generate either 0,1,2,3,4 or 5. random.nextInt(6)也将生成0(包括)和6(不包括)之间的整数,因此将生成0,1,2,3,4或5。

So to generate a number 1-5 (inclusive) you'll have to do random.nextInt(5)+1 因此要生成1-5(包括)数字,您必须执行random.nextInt(5)+1

So the for loop will become: 所以for循环将变为:

for (int i=0; i<populationArray.length; i++){
        for (int j=0; j<populationArray[i].length; j++) {
            populationArray[i][j] = random.nextInt(5)+1;
            populationArray[j][i] = populationArray[i][j];
    }
}

However I'd like to point out that all of the positions are given a random value twice. 但是我想指出所有的位置都是两次随机值。 For creating a single 6x6 array the difference won't be very noticeable. 对于创建单个6x6阵列,差异不会非常明显。 But if you are planning to create bigger/many arrays I'd advise to optimise your code to avoid giving each spot in the array a value twice. 但是如果你打算创建更大/更多的数组,我建议优化你的代码,以避免给数组中的每个点赋值两次。

You can optimize this by changing j<populationArray[i].length to j<=i : 你可以通过将j<populationArray[i].length更改为j<=i来优化它:

for (int i=0; i<populationArray.length; i++){
    for (int j=0; j<=i; j++) {
        populationArray[i][j] = random.nextInt(5)+1;
        populationArray[j][i] = populationArray[i][j];
    }
}

Your issue is with these lines 你的问题是这些问题

populationArray[i][j] = random.nextInt(6);
if(populationArray[i][j] != 0) {
    populationArray[j][i] = populationArray[i][j];
}

if populationArray[i][j] = random.nextInt(6); 如果populationArray[i][j] = random.nextInt(6); returns 0 than it will not be reflected. 返回0比不反映。 I suspect you want populationArray[i][j] = random.nextInt(5)+1; 我怀疑你想要populationArray[i][j] = random.nextInt(5)+1; this will return a random number between 1 and 6. 这将返回1到6之间的随机数。

Bonus: your code will now work but it is actually writing each cell twice. 额外奖励:您的代码现在可以正常工作,但它实际上是每次写入两次单元格。 It will write [1, 4] and mirror to [4, 1] but then it will continue looping and write over [4, 1] and mirror to [1, 4] to fix this you will want to add a check to see if it's 0 before writing to it. 它会写[1,4]并镜像到[4,1]但是它会继续循环并写入[4,1]并镜像到[1,4]来解决这个问题你需要添加一个检查来查看如果它在写入之前为0。 Which coincidentally just ends up moving your if statement one line up. 巧合的是,最终将你的if语句移动一行。

    for (int i=0; i<populationArray.length; i++){
        for (int j=i; j<populationArray[i].length; j++) {
            populationArray[i][j] = random.nextInt(5)+1;
            populationArray[j][i] = populationArray[i][j];
        }
    }

EDIT: changed the solution based on Luke's comment. 编辑:根据卢克的评论改变了解决方案。 Note the change to int j=i on the second for statement. 请注意第二个for语句对int j=i的更改。

In case your arrays get larger you might want to save time by not setting each element twice. 如果您的阵列变大,您可能希望通过不设置每个元素两次来节省时间。 Therefore your inner loop should not run until populationArray[i].length 因此,你的内部循环不应该运行,直到populationArray[i].length

for (int i=0; i<populationArray.length; i++){
        for (int j=0; j<populationArray[i].length-i; j++) {
            populationArray[i][j] = random.nextInt(6);
            int oppositI = populationArray.length -1 - i;
            int oppositJ = populationArray[i].length -1 - j;
            populationArray[oppositI][oppositJ] = populationArray[i][j];
    }
}

This assumes that your populationArray is a sqare. 这假设您的populationArray是一个sqare。 It will be kinda more complex otherwise... 否则会更复杂......

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

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