Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-24-2011, 10:07 AM
funkygarzon
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
• 01-24-2011, 10:31 AM
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.

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
Code:

```for(int i = 0; i < arr.length; i++){   arr[i][i] = k++; }```
What do you think would happen here?
• 01-24-2011, 11:09 AM
funkygarzon
Quote:

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.

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
Code:

```for(int i = 0; i < arr.length; i++){   arr[i][i] = k++; }```
What do you think would happen here?

my doubt is
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 :(
• 01-24-2011, 11:16 AM
sunde887
ah, I see, Id imagine it's because you don't have to use a statement block

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.
• 01-24-2011, 11:28 AM
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:
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:
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-
• 01-24-2011, 11:31 AM
funkygarzon
Quote:

Originally Posted by sunde887
ah, I see, Id imagine it's because you don't have to use a statement block

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 :( .
• 01-24-2011, 01:25 PM
demoniac
sunde887 was right, even if he clarified himself a bit loopy :p

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":
Code:

`for(j=0; j<5; j++) {twoD[i][j] = k; k++;} <-- It could also be written this way`
• 01-24-2011, 01:50 PM
sunde887
Sometimes I tend to get like 50 thoughts at a time and express them poorly, I apologize.
• 01-24-2011, 01:56 PM
funkygarzon
Quote:

Originally Posted by demoniac
sunde887 was right, even if he clarified himself a bit loopy :p

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":
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 ?

Quote:

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:
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:
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
• 01-24-2011, 02:01 PM
funkygarzon
Quote:

Originally Posted by demoniac
sunde887 was right, even if he clarified himself a bit loopy :p

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":
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 ,

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];```
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
Code:

```twoD[0] = new int[5]; twoD[1] = new int[5]; twoD[2] = new int[5]; twoD[3] = new int[5];```
codes really works ?
• 01-24-2011, 02:13 PM
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
Code:

`int[][] chess = new int[8][8];`
That would be like a chessboard
if you did
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:
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.
• 01-24-2011, 02:14 PM
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:
Code:

`        int[][] twoD = new int[3][5];`
...is the same as this:
Code:

```        int[][] twoD = new int[3][];         for (int i = 0; i < 3; i++) {                 twoD[i] = new int[5];         }```
...which is the same as this:
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:
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-
• 01-24-2011, 02:41 PM
funkygarzon
Quote:

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
Code:

`int[][] chess = new int[8][8];`
That would be like a chessboard
if you did
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:
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 :)

Quote:

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:
Code:

`        int[][] twoD = new int[3][5];`
...is the same as this:
Code:

```        int[][] twoD = new int[3][];         for (int i = 0; i < 3; i++) {                 twoD[i] = new int[5];         }```
...which is the same as this:
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:
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
• 01-24-2011, 02:51 PM
sunde887
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:
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
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(); }```
• 01-24-2011, 04:53 PM
funkygarzon
Quote:

Originally Posted by sunde887
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:
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
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 :) .
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 :
Code:

```1 2 3 4 5 6 7 8```
now new doubt a raised for me buddy , sorry ,
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
• 01-24-2011, 06:29 PM
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
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.

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
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.
• 01-25-2011, 07:29 AM
funkygarzon
Quote:

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

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.

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 :(

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
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 ? :(
• 01-25-2011, 07:45 AM
JosAH
Quote:

Originally Posted by funkygarzon
particularly this line
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:

Code:

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

Jos
• 01-25-2011, 07:49 AM
funkygarzon
Quote:

Originally Posted by JosAH
It's a mistake/typo; remove the parentheses:

Code:

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

Jos

oooh wooooooow that s great JosAH , thanks a lot ..now i am free from all doubt ... :):):):):):)
• 01-25-2011, 09:55 AM
funkygarzon
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

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
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last