Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Mirroring' a two-dimensional array [Java]

Tags:

java

arrays

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). 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. 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):

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.

like image 391
Juniper Avatar asked Feb 06 '26 05:02

Juniper


1 Answers

Because of the if(populationArray[i][j] != 0) only the values that aren't 0 are mirrored. if you remove the if statment the code will work.

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.

So to generate a number 1-5 (inclusive) you'll have to do random.nextInt(5)+1

So the for loop will become:

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

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];
    }
}
like image 154
Henrykvdb Avatar answered Feb 07 '26 20:02

Henrykvdb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!