# Thread: Retrieve Multidimensional Array ??

1. Member
Join Date
Dec 2009
Posts
10
Rep Power
0

## Retrieve Multidimensional Array ??

I need to retrieve multidimensional array. How do I do it ?

Java 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.

2. Java 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?

3. Member
Join Date
Dec 2009
Posts
10
Rep Power
0
Could you please explain the logic in second for loop
Java Code:
`..j < ages2[i].length...`

4. 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.

#### Posting Permissions

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