Results 1 to 4 of 4
  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    176
    Rep Power
    1

    Default Java Length of One Dimension From A Multidimensional Array

    So you know how you can do array.length, I have an array with [][][][], and what do I do to find the length of one section?

    So let's say
    Java Code:
    int array[][] = {
    	{5, 5, 5, 5},
    	{4, 4, 4, 4},
    	{3, 3, 3, 3}
    }
    How do I find out the length of the {5, 5, 5, 5} which is 4. Because there are 4 place-holders.
    Is there a way??

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Java Length of One Dimension From A Multidimensional Array

    Multidimensional arrays in Java are nothing more than arrays referring to arrays referring to arrays. So you can actually reference a "2D" array as
    a "1D" array:

    Java Code:
    int[][] a;  // allocated and initialized somewhere else.
    for (int j = 0; j < a.length; j++) {
        System.out.println(a[j].length);
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Java Length of One Dimension From A Multidimensional Array

    Your two dimensional array is an array of arrays and you could do something like array[1].length

    With kind regards,

    Konrad

  4. #4
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    176
    Rep Power
    1

    Default Re: Java Length of One Dimension From A Multidimensional Array

    Thanks.

Similar Threads

  1. Single to multi-dimension array
    By Migy in forum New To Java
    Replies: 2
    Last Post: 04-05-2011, 07:12 PM
  2. java Array.length property
    By X-ion in forum New To Java
    Replies: 0
    Last Post: 04-07-2010, 09:45 AM
  3. Replies: 5
    Last Post: 05-18-2009, 01:42 PM
  4. noob: two-dimension array
    By bobmasta5 in forum New To Java
    Replies: 5
    Last Post: 03-16-2009, 12:42 AM
  5. Arraylist to a 2- dimension array conversion
    By mars123 in forum New To Java
    Replies: 1
    Last Post: 12-06-2007, 12:24 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
  •