# Length of an array !

• 12-25-2012, 03:46 AM
Jack_Tauson_Sr
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
• 12-25-2012, 03:54 AM
Sierra
Re: Length of an array !
You may get the first dimension with s.length and the second one with s[0].length :)
• 12-25-2012, 04:00 AM
Jack_Tauson_Sr
Re: Length of an array !

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
You may get the first dimension with s.length and the second one with s[0].length :)

• 12-25-2012, 04:32 AM
ieee488
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.
• 12-25-2012, 04:34 AM
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:

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.
• 12-25-2012, 06:40 AM
DarrylBurke
Re: Length of an array !
Quote: