Java for 循环未正确填充数组

Java for loops not populating array correctly

我有以下代码:

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

循环内的 println 检查正确地报告存储的数据,如您所见,如果您 运行 程序。但是,在循环之后,如果我尝试检索循环内设置的任何值,所有值 = 0。我错过了什么?

在循环中设置,所有值都应对应数组第一维的索引,如下所示:

所有解决方案[0][x][x][x][x] = 0;

所有解决方案[1][x][x][x][x] = 1;

所有解决方案[2][x][x][x][x] = 2;

等等...

您永远不会为 allsolutions[4][2][1][4][5] 或正在打印的其他 4 个数组位置中的任何一个分配任何内容,因此它们保持为 0。您的数组中只有 4 个嵌套循环和 5 个维度。

您只能为第二个索引等于第五个索引的位置赋值。例如,如果您尝试打印 System.out.println(allsolutions[4][2][1][4][2]);,您将看到一个非零值。

您可能应该使用 5 个嵌套循环而不是重新使用 j 索引:

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

您缺少一个内部循环。请尝试以下操作:

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

问题源于您上次使用 j 而不是新变量的索引,例如 m.

您在重复 j,因此您在 x1 != x2 时没有填充 [?][x1][?][?][x2]

如果值取决于索引,为什么不使用函数?

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
}

并使用它:

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

您可以将其扩展为更复杂的东西,而无需静态存储所有数据。

它对我有用:

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

}

}

下面的代码输出正确的结果