Nemo
Nemo

Reputation: 15

Initialising a multi dimensional array in Java

I just don't understand how this code is working. I went through it with a pen and paper and it seems to only initialise the first column, but when I run it it works as expected. I'm clearly missing something. Could someone explain this like I'm five? (I understand the difference between array.length and 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]);
        }
        }

}
}

Upvotes: 0

Views: 75

Answers (4)

kpearlkmanic
kpearlkmanic

Reputation: 84

 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

Hope this will help you understand better

Upvotes: 0

Turing85
Turing85

Reputation: 20185

The outer for loop runs over the 1st dimension of the array, the inner for loop runs over the 2nd dimension of the array. I added some output. Should be self-explanatory then.

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

}

Upvotes: 1

Eran
Eran

Reputation: 393831

This line initializes the value in the i'th row and j'th column :

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

Since it appears inside a nested loop, which goes over all valid combinations of i and j (assuming all the rows have the same length), it initializes the entire array.

Upvotes: 0

user1907906
user1907906

Reputation:

Inside the two for loops, the first over the rows, the second over each field in a ro, this line sets a value:

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

The important thing is

twoDArray[i][j]=

Since i is the current row and j is the current column, every field is initialized.

Since in a 2D array every row has the same number of columns, it is save to loop like this:

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

Don't let the [0] distract you here.

An improvement would be

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

Upvotes: 0

Related Questions