# Thread: Length of an array !

1. Senior Member
Join Date
Nov 2011
Posts
165
Rep Power
9

## 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. AN21XX
Join Date
Mar 2012
Location
Munich
Posts
300
Rep Power
9

## Re: Length of an array !

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

3. Senior Member
Join Date
Nov 2011
Posts
165
Rep Power
9

## 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?

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

4. Member
Join Date
Dec 2012
Posts
12
Rep Power
0

## 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. Senior Member
Join Date
Jun 2007
Location
Bali, Indonesia
Posts
762
Rep Power
13

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

7. Senior Member
Join Date
Nov 2011
Posts
165
Rep Power
9