Results 1 to 4 of 4
  1. #1
    oneofthelions is offline Member
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #3
    oneofthelions is offline Member
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Default

    Could you please explain the logic in second for loop
    Java Code:
    ..j < ages2[i].length...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

Similar Threads

  1. COG-145: IBM Cognos 8 BI Multidimensional Author
    By Nacy in forum Reviews / Advertising
    Replies: 0
    Last Post: 11-27-2009, 11:44 AM
  2. Navigate through a multidimensional array
    By VinTiger in forum New To Java
    Replies: 4
    Last Post: 05-05-2009, 08:16 PM
  3. [SOLVED] Multidimensional array
    By Torgero in forum New To Java
    Replies: 20
    Last Post: 03-23-2009, 12:36 AM
  4. Multidimensional hashtable?
    By jklsemicolon in forum New To Java
    Replies: 6
    Last Post: 08-17-2008, 06:23 AM
  5. Multidimensional arrays
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-05-2007, 06:07 PM

Posting Permissions

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