1. Senior Member
Join Date
Mar 2009
Posts
126
Rep Power
0

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++) /* i could not understand how this "for loop" works .please make me clear guys */
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();
}
}
}```
OUTPUT :

0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
Last edited by funkygarzon; 01-24-2011 at 11:10 AM.

2. use code tags when posting code it makes it easier to read

However, you are populating and printing a 2d array, so one loop is for the array of array position, and the other is populating each array in the array.

When thinking of a 2d array try thinking of a chess board, the first loop points to the current row and the second loop puts a value in each square.

Since its a 4 by 5 array, there is an array with 4 items, and each item in the array contains another array with 5 items.

So the first loop will be array[0][another array]

the second loop will fill another array and it continues until each item in the array is filled.

Java Code:
```array[4][5]:

array[0][0] = 0
array[0][1] = 1
array[0][2] = 2
...
array[0][5] = 5
...
array[4][5] = 19```
Another way to clarify is this, imagine only having one loop
Java Code:
```for(int i = 0; i < arr.length; i++){
arr[i][i] = k++;
}```
What do you think would happen here?

3. Senior Member
Join Date
Mar 2009
Posts
126
Rep Power
0
Originally Posted by sunde887
use code tags when posting code it makes it easier to read

However, you are populating and printing a 2d array, so one loop is for the array of array position, and the other is populating each array in the array.

When thinking of a 2d array try thinking of a chess board, the first loop points to the current row and the second loop puts a value in each square.

Since its a 4 by 5 array, there is an array with 4 items, and each item in the array contains another array with 5 items.

So the first loop will be array[0][another array]

the second loop will fill another array and it continues until each item in the array is filled.

Java Code:
```array[4][5]:

array[0][0] = 0
array[0][1] = 1
array[0][2] = 2
...
array[0][5] = 5
...
array[4][5] = 19```
Another way to clarify is this, imagine only having one loop
Java Code:
```for(int i = 0; i < arr.length; i++){
arr[i][i] = k++;
}```
What do you think would happen here?

my doubt is
Java Code:
```for(i=0; i<4; i++) /* i could not understand how this "for loop" works .please make me clear guys */
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;```
how this "for loop "is working ? i mean their is no curly braces "{" after "for(i=0;i<4;i++)" and the curly braces starts only from "for(j=0;j<5;j++)" . my doubt is how the i value is getting incremented without being inside curly braces ? . i hope you understand me :(

4. ah, I see, Id imagine it's because you don't have to use a statement block

Java Code:
```for(int i = 0; i < 5; i++)
System.out.println(i);```
works, so I guess since the only expression after the first for loop is another for loop it works correctly, I could be wrong, and if I am I'm sure someone will correct me, but I'm fairly confident this is why.

5. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
8
If a for loop is not followed by braces, then its body is simply the next statement -- in this case, that statement is the next for loop. Note that this is legal syntax, but bad and confusing style.

This:
Java Code:
```        for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
// more code here```
...is the same as this:
Java Code:
```        for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
twoD[i][j] = k++; // no need for the extra increment statement
}
}
// more code here```
Style and indenting are important, especially when you are starting out. Note the space after the for keyword -- it's not a method. Note the spaces around the assignment and comparison operators -- they make the code easier to read.

-Gary-

6. Senior Member
Join Date
Mar 2009
Posts
126
Rep Power
0
Originally Posted by sunde887
ah, I see, Id imagine it's because you don't have to use a statement block

Java Code:
```for(int i = 0; i < 5; i++)
System.out.println(i);```
works, so I guess since the only expression after the first for loop is another for loop it works correctly, I could be wrong, and if I am I'm sure someone will correct me, but I'm fairly confident this is why.

Sorry buddy i could not understand still :( .

7. sunde887 was right, even if he clarified himself a bit loopy :p

Java Code:
```for(i=0; i<4; i++) <-- This takes ONLY the next line as a body of the loop

for(j=0; j<5; j++) {          \
twoD[i][j] = k;                \   all this
k++;                           /   is that next line
}                             /```
If you dont know what I mean by "all this is that next line":
Java Code:
`for(j=0; j<5; j++) {twoD[i][j] = k; k++;} <-- It could also be written this way`

8. Sometimes I tend to get like 50 thoughts at a time and express them poorly, I apologize.

9. Senior Member
Join Date
Mar 2009
Posts
126
Rep Power
0
Originally Posted by demoniac
sunde887 was right, even if he clarified himself a bit loopy :p

Java Code:
```for(i=0; i<4; i++) <-- This takes ONLY the next line as a body of the loop

for(j=0; j<5; j++) {          \
twoD[i][j] = k;                \   all this
k++;                           /   is that next line
}                             /```
If you dont know what I mean by "all this is that next line":
Java Code:
`for(j=0; j<5; j++) {twoD[i][j] = k; k++;} <-- It could also be written this way`
wooooooooooooooooooooooooooooooooooooooooooooow amazing this is what i want buddy , i clearly understood demoniac . now i wonder how this "for(i=0;i<4;i++) " loop is getting incremented without any curly brace when j becomes j=4 ?

Originally Posted by gcalvin
If a for loop is not followed by braces, then its body is simply the next statement -- in this case, that statement is the next for loop. Note that this is legal syntax, but bad and confusing style.

This:
Java Code:
```        for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
// more code here```
...is the same as this:
Java Code:
```        for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
twoD[i][j] = k++; // no need for the extra increment statement
}
}
// more code here```
Style and indenting are important, especially when you are starting out. Note the space after the for keyword -- it's not a method. Note the spaces around the assignment and comparison operators -- they make the code easier to read.

-Gary-
wooooooow thanks buddy this was another wonderful explanation ..you just put the curly brace and made me understand ,... thanks again

10. Senior Member
Join Date
Mar 2009
Posts
126
Rep Power
0
Originally Posted by demoniac
sunde887 was right, even if he clarified himself a bit loopy :p

Java Code:
```for(i=0; i<4; i++) <-- This takes ONLY the next line as a body of the loop

for(j=0; j<5; j++) {          \
twoD[i][j] = k;                \   all this
k++;                           /   is that next line
}                             /```
If you dont know what I mean by "all this is that next line":
Java Code:
`for(j=0; j<5; j++) {twoD[i][j] = k; k++;} <-- It could also be written this way`

i got another doubt in "for loop " , so please check below code and explain me buddy ,

Java Code:
``` When you allocate memory for a multidimensional array, you need only specify the
memory for the first (leftmost) dimension. You can allocate the remaining dimensions
separately. For example, this following code allocates memory for the first dimension of
twoD when it is declared. It allocates the second dimension manually.
int twoD[][] = new int[4][];
twoD[0] = new int[5];
twoD[1] = new int[5];
twoD[2] = new int[5];
twoD[3] = new int[5];```
Java Code:
`int twoD[][] = new int[4][];`
what is the meaning for this and how we have visualize this in a "chess board " like sunde887 said ?

and how these
Java Code:
```twoD[0] = new int[5];
twoD[1] = new int[5];
twoD[2] = new int[5];
twoD[3] = new int[5];```
codes really works ?
Last edited by funkygarzon; 01-24-2011 at 02:04 PM.

11. My chessboard example was just something I used when I first encountered 2d arrays, for me it clarifies them a bit. It's more of how I visualize a 2d array in my head. an array like
Java Code:
`int[][] chess = new int[8][8];`
That would be like a chessboard
if you did
Java Code:
```for(int i = 0; i < arr.length; i++){
for(int  j = 0; j < arr[i].length; j++){
arr[i][j] = 1;
}
}

for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length; j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}```
output would be:
Java Code:
```1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1```
64 items, just like an 8x8 chessboard would have

The loop increments i because it's how a for loop works, it continues until the condition is no longer met. When j reaches 4, the second loop is finished, so the statement for the first loop is finished and i can increment.

Try thinking of a multi-dimensional array as a single array, normally an array would contain numbers, or some other data. In a multi-dimensional array, each element conatains an int array.

So you are allowed to declare the size of each item after the initial array declaration.

I hope I am being clear.
Last edited by sunde887; 01-24-2011 at 02:20 PM.

12. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
8
This is subtle and confusing, but in a sense, there is really no such thing as a "multi-dimensional" array. What you really have are arrays of arrays. So this:
Java Code:
`        int[][] twoD = new int[3][5];`
...is the same as this:
Java Code:
```        int[][] twoD = new int[3][];
for (int i = 0; i < 3; i++) {
twoD[i] = new int[5];
}```
...which is the same as this:
Java Code:
```        int[][] twoD = new int[3][];
twoD[0] = new int[5];
twoD[1] = new int[5];
twoD[2] = new int[5];```
You could alternatively do something like this:
Java Code:
```        int[][] twoD = new int[3][];
twoD[0] = new int[2];
twoD[1] = new int[10];
twoD[2] = new int[6];```
All these things are legal, but they're very unusual, and not worth thinking about all that much. Stay away from weird, confusing code, and if you must do something weird, comment it profusely.

-Gary-

13. Senior Member
Join Date
Mar 2009
Posts
126
Rep Power
0
Originally Posted by sunde887
My chessboard example was just something I used when I first encountered 2d arrays, for me it clarifies them a bit. It's more of how I visualize a 2d array in my head. an array like
Java Code:
`int[][] chess = new int[8][8];`
That would be like a chessboard
if you did
Java Code:
```for(int i = 0; i < arr.length; i++){
for(int  j = 0; j < arr[i].length; j++){
arr[i][j] = 1;
}
}

for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length; j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}```
output would be:
Java Code:
```1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1```
64 items, just like an 8x8 chessboard would have

The loop increments i because it's how a for loop works, it continues until the condition is no longer met. When j reaches 4, the second loop is finished, so the statement for the first loop is finished and i can increment.

Try thinking of a multi-dimensional array as a single array, normally an array would contain numbers, or some other data. In a multi-dimensional array, each element conatains an int array.

So you are allowed to declare the size of each item after the initial array declaration.

I hope I am being clear.

oh sunde887 , that was great buddy ...thanks for making me clear ..thank you :)

Originally Posted by gcalvin
This is subtle and confusing, but in a sense, there is really no such thing as a "multi-dimensional" array. What you really have are arrays of arrays. So this:
Java Code:
`        int[][] twoD = new int[3][5];`
...is the same as this:
Java Code:
```        int[][] twoD = new int[3][];
for (int i = 0; i < 3; i++) {
twoD[i] = new int[5];
}```
...which is the same as this:
Java Code:
```        int[][] twoD = new int[3][];
twoD[0] = new int[5];
twoD[1] = new int[5];
twoD[2] = new int[5];```
You could alternatively do something like this:
Java Code:
```        int[][] twoD = new int[3][];
twoD[0] = new int[2];
twoD[1] = new int[10];
twoD[2] = new int[6];```
All these things are legal, but they're very unusual, and not worth thinking about all that much. Stay away from weird, confusing code, and if you must do something weird, comment it profusely.

-Gary-
nice explanation buddy , but i am like understood but not so clearly understood .

please tell me according to this below code
int[][] twoD = new int[3][];
twoD[0] = new int[2];
twoD[1] = new int[10];
twoD[2] = new int[6];

where " twoD[1] = new int[10]; " this "10" allocation happens . i mean in which row and in which column ? sorry for my noob question and for my bad english

14. Java Code:
```int[][] twoD = new int[3][];
twoD[0] = new int[2];
twoD[1] = new int[10];
twoD[2] = new int[6];```
with this code, each element in the array twoD contains an array, so the element at 0 has an array containing 2 items, the element at 1 contains an array with 10 items, and element 3 contains an array with 6 items

The array would print out as:
Java Code:
```1 2
3 4 5 6 7 8 9 10 11 12
13 14 15 16 17 18```
and you can print it with this code
Java Code:
```int[][] arr = new int[3][];
arr[0] = new int[2];
arr[1] = new int[10];
arr[2] = new int[6];
int k = 1;

//instantiate items in array
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length; j++){
arr[i][j] = k++;
}
}

//print items
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length; j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}```

15. Senior Member
Join Date
Mar 2009
Posts
126
Rep Power
0
Originally Posted by sunde887
Java Code:
```int[][] twoD = new int[3][];
twoD[0] = new int[2];
twoD[1] = new int[10];
twoD[2] = new int[6];```
with this code, each element in the array twoD contains an array, so the element at 0 has an array containing 2 items, the element at 1 contains an array with 10 items, and element 3 contains an array with 6 items

The array would print out as:
Java Code:
```1 2
3 4 5 6 7 8 9 10 11 12
13 14 15 16 17 18```
and you can print it with this code
Java Code:
```int[][] arr = new int[3][];
arr[0] = new int[2];
arr[1] = new int[10];
arr[2] = new int[6];
int k = 1;

//instantiate items in array
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length; j++){
arr[i][j] = k++;
}
}

//print items
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length; j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}```
wooooooooooooooooooooooooooooooooooooooooooooooooo w buddy thank you very much , you made me understand this clearly :) .
Java Code:
```int[][] twoD = new int[3][];
twoD[0] = new int[1];
twoD[1] = new int[5];
twoD[2] = new int[2];```
so for the above code ,
OUTPUT IS THIS RIGHT :
Java Code:
```1
2 3 4 5 6
7 8```
now new doubt a raised for me buddy , sorry ,
Java Code:
`arr.length`
how does the compiler is taking the length correctly if we give "arr.length" ? and what is the difference between "arr.length " and " arr[i].length" ? ...sorry for too much request sunde887 ...thanks in advance
Last edited by funkygarzon; 01-24-2011 at 05:02 PM.

16. Haha, I don't mind, ask as much as you want, I refresh these forums way more than I should

ok, arr.length in a multidimensional array gets the length, put better, it counts how many arrays there are
Java Code:
```int[][] arr = new int[3][];
int[0] = new int[2];
int[1] = new int[5];
int[2] = new int[3];```
in this code, arr.length counts how many i's there are, in this case there is 0, 1, and 2, so arr.length will produce 3.

Now, how would you determine the length of the second piece? you can't use the same number all the time, you need a .length type item. Since each item in the array contains an array arr[i].length will find the length of the array stored in element i.

Java Code:
```int[][] = new int[3][]; //arr.length = 3, it is declared here
int[0] = new int[7];//arr[i].length here means arr[0].length, and in this one its length is 7
int[1] = new int[3];//arr[i].length is 3
int[2] = new int[100];//arr[i](arr[2]).length = 100```
I may be unclear, let me know how you understand this, and if it's confusing I will do my best to clarify it.

I believe you are overcomplicating multidimensional arrays, someone else above said "there really is no such thing as a multi dimensional array, it's actual an array where the elements contain arrays." Thinking like that might help you in understanding. Use the chessboard example for picturing what they look like, and use this description to think how they are defined.

Think about an array of strings, if you wanted to loop through each string in the array with str.substring, what would you do?

its basically the same as a 2d array
Java Code:
```for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length() - 1; j++){
arr[i].substring(i, i + 1);
}
}```
If this extra part is confusing just ignore it and focus on the stuff above it.
Last edited by sunde887; 01-24-2011 at 06:36 PM.

17. Senior Member
Join Date
Mar 2009
Posts
126
Rep Power
0
Originally Posted by sunde887
Haha, I don't mind, ask as much as you want, I refresh these forums way more than I should

ok, arr.length in a multidimensional array gets the length, put better, it counts how many arrays there are

Java Code:
```int[][] arr = new int[3][];
int[0] = new int[2];
int[1] = new int[5];
int[2] = new int[3];```
in this code, arr.length counts how many i's there are, in this case there is 0, 1, and 2, so arr.length will produce 3.

Now, how would you determine the length of the second piece? you can't use the same number all the time, you need a .length type item. Since each item in the array contains an array arr[i].length will find the length of the array stored in element i.

Java Code:
```int[][] = new int[3][]; //arr.length = 3, it is declared here
int[0] = new int[7];//arr[i].length here means arr[0].length, and in this one its length is 7
int[1] = new int[3];//arr[i].length is 3
int[2] = new int[100];//arr[i](arr[2]).length = 100```
wooooooooooooooooooooooooooooooooooooooooooooooooo ooooooooooooow wonderful great explanation - i clearly i understood the difference buddy . :) . thank a lot for spending time for making me understand clearly :)

now i could not figure out the result for the below code which you gave as string example :(

Java Code:
```for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length() - 1; j++){
arr[i].substring(i, i + 1);
}
}```
particularly this line
Java Code:
`arr[i].length() - 1`
i haven 't seen like this before ? what is the meaning for this "arr[i].length() - 1 " and why you are using method " () " here ? :(
Last edited by funkygarzon; 01-25-2011 at 07:31 AM.

18. Originally Posted by funkygarzon
particularly this line
Java Code:
`arr[i].length() - 1`
i haven 't seen like this before ? what is the meaning for this "arr[i].length() - 1 " and why you are using method " () " here ? :(
It's a mistake/typo; remove the parentheses:

Java Code:
`arr[i].length-1`
kind regards,

Jos

19. Senior Member
Join Date
Mar 2009
Posts
126
Rep Power
0
Originally Posted by JosAH
It's a mistake/typo; remove the parentheses:

Java Code:
`arr[i].length-1`
kind regards,

Jos

oooh wooooooow that s great JosAH , thanks a lot ..now i am free from all doubt ... :):):):):):)

20. Senior Member
Join Date
Mar 2009
Posts
126
Rep Power
0
ooohh my dear friends , Another big doubt

1 . what is 3 by 4 by 5 matrix ? i could not visualize the matrix of this below program

Java Code:
```// Demonstrate a three-dimensional array.
class threeDMatrix {
public static void main(String args[]) {
int threeD[][][] = new int[3][4][5];
int i, j, k;
for(i=0; i<3; i++)
for(j=0; j<4; j++)
for(k=0; k<5; k++)
threeD[i][j][k] = i * j * k;
for(i=0; i<3; i++) {
for(j=0; j<4; j++) {
for(k=0; k<5; k++)
System.out.print(threeD[i][j][k] + " ");
System.out.println();
}
System.out.println();
}
}
}```
This program generates the following output:

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12

0 0 0 0 0
0 2 4 6 8
0 4 8 12 16
0 6 12 18 24

Alternative Array Declaration

Page 1 of 2 12 Last

#### Posting Permissions

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