Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Length of an array !

  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    109
    Rep Power
    0

    Default Length of an array !

    How to determine the length of the row and column of a two dimensional array?

    For example:

    I have the following code:

    String[][] s = new String[3][2];


    I understood, how to determine the length of the "row" like the following:

    int i = s.length;
    System.out.println(i);

    This gives me length as 3.

    But how do I determine the length of the "column" just like in above fashion? I want my answer to be 2 because it's length is 2.

    Thanks

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Length of an array !

    You may get the first dimension with s.length and the second one with s[0].length :)
    I like likes!

  3. #3
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    109
    Rep Power
    0

    Default Re: Length of an array !

    Thanks for your answer.

    But I'm confused, what does s[0].length means? s[0] will be the first dimension , I understood it, but is there any way of accessing second dimension directly?

    Quote Originally Posted by Sierra View Post
    You may get the first dimension with s.length and the second one with s[0].length :)

  4. #4
    ieee488 is offline Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0

    Default Re: Length of an array !

    s[0] is a one-dimensional array.
    Since your two-dimensional array is not irregular in size, s[0].length will give you the number of elements.

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Length of an array !

    In the example when you declare an array like String[3][2] it means that you will create a 3 array of string that contains another 2 array of string. If I represent this in data it will be something like this:

    Java Code:
    {
        {"1", "1"},
        {"2", "2"},
        {"3", "3"}
    }
    If you see the data above, the first line of data is the s[0], the second is s[1] and the third is s[2]. So if you want to get the length of those arrays you must give the index of which array you want to find the length.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Length of an array !

    Quote Originally Posted by wsaryada View Post
    In the example when you declare an array like String[3][2] it means that you will create a 3 array of string that contains another 2 array of string.
    More correctly, you create an array of String[] with 3 elements, each of which is an array of String with 2 elements.

    In Java, referring to a multidimensional array is just an easy, short-cut way to express oneself. Java does not have multidimensional arrays per se. Java has arrays; each element of an array may be of a primitive or reference type. Since an array is also a reference type, the elements of an array can be of array type. So a String[] is an array of type String; a String[][] is an array of type String[].

    And an int[][][] is an array with elements of type int[][], each element of which is an array of int[], each element of which is an array of int.

    db
    Sierra likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    109
    Rep Power
    0

    Default Re: Length of an array !

    Thank you very much everyone for your answers. Appreciated your time. :)

Similar Threads

  1. How to declare array of Unknown Length for below code
    By sandeep43 in forum New To Java
    Replies: 10
    Last Post: 08-24-2011, 01:31 PM
  2. Trying to display the length of the array using tags
    By Ms.Ranjan in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-03-2010, 12:15 AM
  3. java Array.length property
    By X-ion in forum New To Java
    Replies: 0
    Last Post: 04-07-2010, 08:45 AM
  4. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  5. Using reflection to check array type and length
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:42 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
  •