• September 4th, 2011, 08:51 AM
kaan
Hello Experts!

I am new to this forum.
Could you please tell me how this multi-dimensional array works? How it corresponds to differents style of outputs format like triangle shape of elements output?
Code Java:

```class TwoDAgain { public static void main(String args[]) { int twoD[][] = new int[4][]; twoD[0] = new int[1]; twoD[1] = new int[2]; twoD[2] = new int[3]; twoD[3] = new int[4]; int i, j, k = 0; for(i=0; i<4; i++) for(j=0; j<i+1; j++) { twoD[i][j] = k; k++; } for(i=0; i<4; i++) { for(j=0; j<i+1; j++) System.out.print(twoD[i][j] + " "); System.out.println(); } } }```
output:
0
1 2
3 4 5
6 7 8 9

Regards
Kaan
• September 4th, 2011, 09:43 AM
Norm
Quote:

how this multi-dimensional array works
Multidim arrays are really a grouping of single dim arrays.
Each dim of the array can contain an array of a unique dimensions.

Code :

```int twoD[][] = new int[4][]; // Create 2 dim array with 4 slots in the first dim twoD[0] = new int[1]; // set the first array to be a one dim array with 1 slot twoD[1] = new int[2]; // set the second array to be a one dim with 2 slots twoD[2] = new int[3]; // set the third array to be a one dim with 3 slots twoD[3] = new int[4]; // set the forth array to be a one dim with 4 slots```
The valid indexes for the above array would be:
0,0
1,0 1,1
2,0 2,1 2,2
3,0 3,1 3,2 3,3
• September 4th, 2011, 04:56 PM
JavaPF