简体   繁体   中英

Java for loops not populating array correctly

I have the following code:

public class solutionsTest {
    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
        int allsolutions[][][][][] = new int[16][16][16][16][16];

        for (int i = 0; i <= 15; i++) {
            for (int j = 0; j <= 15; j++) {
                for (int k = 0; k <= 15; k++) {
                    for (int l = 0; l <= 15; l++) {
                        allsolutions[i][j][k][l][j] = i;
                        System.out.println("Set index " + i + " " + j + " " + k + " " + l + " " + j + " equal to " + i);
                    }
                }
            }
        }

        System.out.println(allsolutions[4][2][1][4][5]);
        System.out.println(allsolutions[5][2][1][4][5]);
        System.out.println(allsolutions[6][2][1][4][5]);
        System.out.println(allsolutions[7][2][1][4][5]);
        System.out.println(allsolutions[8][2][1][4][5]);
    }
}

The println check inside the loop correctly reports the stored data correctly, as you can see if you run the program. However, after the loops, if I try to retrieve any of the values that were set inside the loops, all of the values = 0. What am I missing?

As set in the loop, all values should correspond to the index of the first dimension of the array as such:

allsolutions[0][x][x][x][x] = 0;

allsolutions[1][x][x][x][x] = 1;

allsolutions[2][x][x][x][x] = 2;

And so on...

You never assign anything to allsolutions[4][2][1][4][5] , or any of the other 4 array positions you are printing, so they remains 0. You only have 4 nested loops and 5 dimensions in your array.

You only assign values to positions where the 2nd index is equal the 5th index. If you try to print, for example, System.out.println(allsolutions[4][2][1][4][2]); , you'll see a non-zero value.

You should probably use 5 nested loop instead of re-using the j index :

for(int i=0;i<=15;i++){
    for(int j=0;j<=15;j++){
        for(int k=0;k<=15;k++){
            for(int l=0;l<=15;l++){
              for(int m=0;m<=15;m++){
                allsolutions[i][j][k][l][m] = i;
                System.out.println("Set index "+ i + " " + j + " " + k + " " + l + " " + m + " equal to " + i);
              }
            }
        }
    }
}

You're missing an inner loop. Try the following:

final int DIM = 16;

int[][][][][] allsolutions = new int[DIM][DIM][DIM][DIM][DIM];

for (int i = 0; i < DIM; i++){
    for (int j = 0; j < DIM; j++) {
        for (int k = 0; k < DIM; k++) {
            for (int l = 0; l < DIM; l++){
                for (int m = 0; m < DIM; m++) {
                    allsolutions[i][j][k][l][m] = i;
                    System.out.println("Set index " + i + " " + j + " " + k + " " + l + " " + m + " equal to " + i);
                }
            }
        }
    }
}

The issue stems from your last index using j instead of a new variable, such as m .

You're repeating j so you're not populating [?][x1][?][?][x2] when x1 != x2 .

If the values are dependant on the indices, why are you not using a function?

public int getValue(int i, int dc1, int dc2, int dc3, int dc4){
    if (i < 16 && 0 <= i){
        return i;
    }
    // ... throw error or return sentinel value
}

And use it:

System.out.println(getValue(4,2,1,4,5)); // 4

You can extend it to something more sophisticated without storing all the data statically.

It worked for me :

import java.util.Arrays;

public class SolutionsTest {

private static int [][][][] allsolutions; 
//make the arrays static and as a field not local

public static void main (String args[]) {

    allsolutions = new int [16][16][16][16];
    //Initialize the arrays

    for (int i = 0; i < 16;i++) { // The i or index loop
        for (int x = 0; x < 16;x++) { 
            for (int y = 0; y < 16;y++) {
                for (int z = 0; z < 16; z++) {
                    allsolutions[i][x][y][z] = i; //set all as index
                }
            }
        }
    }   

    System.out.println(allsolutions[0][0][0][0]); //prints 0
    System.out.println(allsolutions[1][0][0][0]); //prints 1
    System.out.println(allsolutions[2][0][0][0]); //prints 2
    System.out.println(allsolutions[3][0][0][0]); //prints 3
    System.out.println(allsolutions[0][4][5][5]); //prints 0
    System.out.println(allsolutions[1][8][9][10]); //prints 1
    System.out.println(allsolutions[2][9][10][1]); //prints 2

    System.out.println(Arrays.deepToString(allsolutions)); 
    //prints all arrays

}

}

The following code output the right results

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