# Retrieve Multidimensional Array ??

Printable View

• 12-12-2009, 07:39 AM
oneofthelions
Retrieve Multidimensional Array ??
I need to retrieve multidimensional array. How do I do it ?

Code:

```int ages[][] =  {{1,2,3,4,5,6,7,8,9,10},{11,12,13,14,15,16,17,18,19,20}}; for( int i=0; i<ages2.length; i++ ){             for( int j=0; j<ages2.length; j++ ){             System.out.print( ages2[i][j] + " " );             }```
By above code I can only retrieve "1,2,11,12" elements. Not all of them, how can I do it.
• 12-12-2009, 07:43 AM
Fubarable
Code:

```public class Foo1 {   public static void main(String[] args) {       int ages2[][] = {         {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},         {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}       };       for (int i = 0; i < ages2.length; i++) {         for (int j = 0; j < ages2[i].length; j++) {             System.out.print(ages2[i][j] + " ");         }         System.out.println();       }   } }```
the key is in the second for loop use j < ages2[i].length;

Make sense?
• 12-12-2009, 07:59 AM
oneofthelions
Could you please explain the logic in second for loop
Code:

`..j < ages2[i].length...`
• 12-12-2009, 08:24 AM
Fubarable
ages2.length gives you the number of items in the outer arrays, the rows, and here it will be 2.

ages2[i].length gives you the number of items held in the ith inner array.

A 2 dimensional array is not really what it seems to be. It is in fact an array holding inner arrays.

So the first inner array, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} is ages2[0], and the length of this array is ages2[0].length.