# Thread: multidimensional array prob T_T

1. Member
Join Date
Sep 2012
Posts
41
Rep Power
0

## multidimensional array prob T_T

Java Code:
```class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}

}```
this is the output
Java Code:
```0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19```
can someone explain how this code works(in complete detail please :D).

2. ## Re: multidimensional array prob T_T

Please go through Code Conventions for the Java Programming Language: Contents and learn how to format code for readability.

db

3. Senior Member
Join Date
Nov 2012
Posts
258
Rep Power
2

## Re: multidimensional array prob T_T

ok:

Java Code:
`int twoD[][]= new int[4][5];`
The above line of code create an array of arrays (also known as 2d array, or multi dimensional array). This will be structured as follow:

4 "rows"
5 "elements" in each row

Also the contents of each element must be an integer as specified by this line : new int[4][5]

This matches the format of your output

Next nested for loop:

Java Code:
```int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;```
first of all it instantiates the values of integers i, k, and j as 0. int i, j, k = 0;

Now the nested for loop happens, i assume you know about for loops in general. First of all the outer most for loop which counts i is used to iterate through the rows in the the 2d array. so when i = 0, it refers to row 0 (the first row) in the table of out put, (in this case 0 1 2 3 4 = Row 0)

the same applied for the J for loop, but this keeps track of the columns, or elements in said "i" row. This for loop completes before the i value is incremented. The j for loop with execute 5 times before you move onto the next row. (next row means i ++)

Inside the J for loop the value are assigned to the array

Java Code:
```twoD[i][j] = k;
k++;```
twoD[i][j] refers to the location in the 2D array, twoD[0][0] refers to the very first value in the array. Then you set the value at this location to the value of k (which is currently 0).

At this stage this means that the lines:
Java Code:
```twoD[i][j] = k;
k++;```
translates to:
Java Code:
```twoD[0][0] = 0; //i = 0, j = 0 and k = 0 AT THIS POINT
k++;```

Now the k value is increased by one using k++ so Now k = 1 Also the J for loop has completed one cycle so the J value increases, J Value is now 1

Lets run the code again

At this stage this means that the lines:
Java Code:
```twoD[i][j] = k;
k++;```
translates to:
Java Code:
```twoD[0][1] = 1; //i = 0, j = 1 and k = 1 AT THIS POINT
k++;```

So now the value at [0][1] will be 1, [0][1] is the 2nd element in the first row (remember Java counts from 0!) Then we increment J and K again.

One this has happened 5 times, we increment i (i++) and execute the J for loop another 5 times

now that i has been incremented the code is as follows:

At this stage this means that the lines:
Java Code:
```twoD[i][j] = k;
k++;```
translates to:
Java Code:
```twoD[1][0] = 5; //i = 1, j = 0 and k = 5 AT THIS POINT
k++;```
And the pattern happens again (see above explanation)

This cycle continues till the I value equals 3 and the J value = 4 (remember java counts from 0, so i = 3 means 4 rows, j = 4 means 5 columns)

Then the code simply prints out the value at every point in the grid.

Printing example
Java Code:
```System.out.print(twoD[i][j] + " ");
System.out.println(); // PRINTS A BLANK LINE```

for the first element in the array

Java Code:
```System.out.print(twoD[0][0] + " "); // i and j start off as 0
System.out.println();```
this will look in the 2D Array and look up the value at [0][0] ([row][column]) and simply print it out to the console.
This will again happen the same amount of times as the previous explanation.

Hope this explains everything

4. Member
Join Date
Sep 2012
Posts
41
Rep Power
0

## Re: multidimensional array prob T_T

Thank you for your explanation and now i understand this code. i will comment again if i have any questions.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•