在Java中初始化多维数组

时间:2015-04-09 12:32:38

标签: java arrays multidimensional-array

我只是不明白这段代码是如何工作的。我用笔和纸来完成它,它似乎只是初始化第一列,但是当我运行它时,它按预期工作。我显然遗漏了一些东西。有人可以像我五岁那样解释这个吗? (我理解array.length和array [0] .length)

之间的区别
    public static void main(String[] args) {
        final int rows=10,columns=5;
        int[][] twoDArray = new int [rows][columns];

        twoDArray[0][0]=0;
        twoDArray[0][1]=1;

        for (int i =0;i<twoDArray.length;++i){
            for (int j =0;j<twoDArray[0].length;++j){
                twoDArray[i][j]=i*twoDArray[0].length+j;
            }
        }


        for (int i =0;i<twoDArray.length;++i)
        {   for (int j=0;j<twoDArray[0].length;++j)
        {
            System.out.println("The element at twoDArray["+i+"]"+"["+j+"] is: " + twoDArray[i][j]);
        }
        }

}
}

4 个答案:

答案 0 :(得分:1)

外部for循环遍历数组的第一维,内部for循环遍历数组的第二维。我添加了一些输出。那应该是不言自明的。

public static void main(String[] args) {
    final int rows=10,columns=5;
    int[][] twoDArray = new int [rows][columns];

    twoDArray[0][0]=0;
    twoDArray[0][1]=1;

    for (int i =0;i<twoDArray.length;++i){
        for (int j =0;j<twoDArray[0].length;++j){
            System.out.println("Row: " + i + ", Col: " + j);
            twoDArray[i][j]=i*twoDArray[0].length+j;
        }
    }


    for (int i =0;i<twoDArray.length;++i) {
        for (int j=0;j<twoDArray[0].length;++j) {
            System.out.println("The element at twoDArray["+i+"]"+"["+j+"] is: " + twoDArray[i][j]);
        }
    }

}

答案 1 :(得分:0)

在两个for循环中,第一个在行上,第二个在一个ro中的每个字段,这一行设置一个值:

twoDArray[i][j] = i*twoDArray[0].length+j;

重要的是

twoDArray[i][j]=

由于i是当前行,j是当前列,因此每个字段都已初始化。

由于在2D数组中每行具有相同的列数,因此将其保存为循环:

for (int j =0;j<twoDArray[0].length;++j){

不要让[0]分散你的注意力。

改进将是

    for (int i=0; i < rows; i++) {
        for (int j=0; j < columns; j++) {
            twoDArray[i][j] = i * twoDArray[0].length + j;
        }
    }

答案 2 :(得分:0)

此行初始化第i行和第j列的值:

twoDArray[i][j]=i*twoDArray[0].length+j;

因为它出现在嵌套循环中,它遍历ij的所有有效组合(假设所有行具有相同的长度),它会初始化整个数组。

答案 3 :(得分:0)

 public static void main(String[] args) {
    final int rows = 10, columns = 5;
    int[][] twoDArray = new int[rows][columns];
    // int[][][] threeDArray = new int[rows][columns][rows];

    twoDArray[0][0] = 0;
    twoDArray[0][1] = 1;
    System.out.println("2Dimenentional Row Length : " + twoDArray.length);
    System.out.println("2Dimentional Column Length : "
            + twoDArray[0].length);

    // System.out.println("3Dimenentional Row Length : " +
    // threeDArray.length);
    // System.out.println("3Dimentional Column Length : "
    // + threeDArray[0].length);
    // System.out.println("3Dimenentional  : " + threeDArray[1].length);

    for (int i = 0; i < twoDArray.length; ++i) {
        for (int j = 0; j < twoDArray[0].length; ++j) {
            twoDArray[i][j] = i * twoDArray[0].length + j;
        }
    }

    for (int i = 0; i < twoDArray.length; ++i) {
        for (int j = 0; j < twoDArray[0].length; ++j) {
            System.out.println("The element at twoDArray[" + i + "]" + "["
                    + j + "] is: " + twoDArray[i][j]);
        }
    }

}

 OutPut:
 2Dimenentional Row Length : 10  
 2Dimentional Column Length : 5
 The element at twoDArray[0][0] is: 0
 The element at twoDArray[0][1] is: 1
 The element at twoDArray[0][2] is: 2
 The element at twoDArray[0][3] is: 3
 The element at twoDArray[0][4] is: 4
 The element at twoDArray[1][0] is: 5
 The element at twoDArray[1][1] is: 6
 The element at twoDArray[1][2] is: 7
 The element at twoDArray[1][3] is: 8
 The element at twoDArray[1][4] is: 9
 The element at twoDArray[2][0] is: 10
 The element at twoDArray[2][1] is: 11
 The element at twoDArray[2][2] is: 12
 The element at twoDArray[2][3] is: 13
 The element at twoDArray[2][4] is: 14
 The element at twoDArray[3][0] is: 15
 The element at twoDArray[3][1] is: 16
 The element at twoDArray[3][2] is: 17
 The element at twoDArray[3][3] is: 18
 The element at twoDArray[3][4] is: 19
 The element at twoDArray[4][0] is: 20
 The element at twoDArray[4][1] is: 21
 The element at twoDArray[4][2] is: 22
 The element at twoDArray[4][3] is: 23
 The element at twoDArray[4][4] is: 24
 The element at twoDArray[5][0] is: 25
 The element at twoDArray[5][1] is: 26
 The element at twoDArray[5][2] is: 27
 The element at twoDArray[5][3] is: 28
 The element at twoDArray[5][4] is: 29
 The element at twoDArray[6][0] is: 30
 The element at twoDArray[6][1] is: 31
 The element at twoDArray[6][2] is: 32
 The element at twoDArray[6][3] is: 33
 The element at twoDArray[6][4] is: 34
 The element at twoDArray[7][0] is: 35
 The element at twoDArray[7][1] is: 36
 The element at twoDArray[7][2] is: 37
 The element at twoDArray[7][3] is: 38
 The element at twoDArray[7][4] is: 39
 The element at twoDArray[8][0] is: 40
 The element at twoDArray[8][1] is: 41
 The element at twoDArray[8][2] is: 42
 The element at twoDArray[8][3] is: 43
 The element at twoDArray[8][4] is: 44
 The element at twoDArray[9][0] is: 45
 The element at twoDArray[9][1] is: 46
 The element at twoDArray[9][2] is: 47
 The element at twoDArray[9][3] is: 48
 The element at twoDArray[9][4] is: 49

希望这有助于您更好地理解