# array length example

Hi,

I am going through below example

http://www.tutorialspoint.com/javaexamples/arrays_upperbound.htm

I wonder how they got output 2, 5
How
data.length) resulted 2
data[0].length) resulted 5
what is significance of 0. can i give 1 instead of 0?
LVL 7
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
This is a multidimensional array. 2 is the length of the first dimension, and the first dimension's first element, (just like the first dimension's second element), each have 5 elements, and so they've called the first dimension's first element's index, (which as you know is zero), and asked it how many elements it has.

And so if you changed the code to this :

``````String[][] data = new String[2][5];
System.out.println("Dimension 1: " + data.length);
System.out.println("Dimension 2: " + data[1].length);
``````

you would get exactly the same output.
Author Commented:
first dimension's first element
that should be called like data[1][1]

1 2 3 5 6
7 4 5 8 0

in above 2*5 array
data[0][0] is 1
data[0][5] is 6
data[1][5] is 0
right
data[1] does not make sense to me is it belong to row count or column count/length?
Commented:
data[1][5] is 0
data[1] is {7, 4, 5, 8, 0}
data[1].length is 5
Commented:
These are the data members :
data[0][0]
data[0][1]
data[0][2]
data[0][3]
data[0][4]

data[1][0]
data[1][1]
data[1][2]
data[1][3]
data[1][4]
Commented:
data[1][5] is 0
No! There is no such element as data[1][5]
Commented:
data[1][5] is 0
should have been
data[1][4] is 0
and
data[0][5] is 6
should have been
data[0][4] is 6
Commented:
gudii :

Please refer to my above comment at 16:35:33ID: 40413746 for a correct breakdown of the array's elements.
Commented:
first dimension's first element

that should be called like data[1][1]

yes- you are still missing something ! What you are missing is, as has been said repeatedly throughout your questions :
**************************************************
ALL arrays, of ANY type, are indexed from 0 as their first element, to length -1 as the last.
**************************************************
Commented:
If that was what was missing, gudii9 would not have said
data[0][0] is 1
What seems to be missing is the distinction between data[1][1] and data[1]

This is independent of the linguistic ambiguity between "first" and "zeroth"
Commented:
Sorry, I don't follow your comment at all. What are you trying to say?

When gudii says he is missing something, I think he means missing a point, not missing data!

Bringing in terms like
This is independent of the linguistic ambiguity between "first" and "zeroth"
is irrelevant and has nothing to do with linguistics or ambiguity of any sort. Indexing is not ambiguous, it is fixed in the language's construction terms, and is as I have indicated in my previous post.
Commented:
Java is unabiguous, English is not unambigous.
Commented:
There's no point entering into an off topic and fruitless examination of this here, but if you want to waste time on it in another appropriate TA then feel free.
Author Commented:
data[0][0]
data[0][1]
data[0][2]
data[0][3]
data[0][4]

data[1][0]
data[1][1]
data[1][2]
data[1][3]
data[1][4]

Above is very clear to me.

data.length) resulted 2
data[0].length) resulted 5
Author Commented:
data[0][5] is 6 this is my mistake it supposed to be data[0][4] is 6
Commented:
data is name of the array.

the length of the array is two.

each of those elements itself has 5 elements.

2 * 5 is ten. there are 10 elements in this composite, multidimensional array.

You already asked, and received an answer to, this same point in your very first comment in this question, and the situation hasn't changed in the meantime. See my first comment.
Commented:
There's no point entering into an off topic
quite agreed.

The array's length is available as a final instance variable length.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.3

data = new String[2][5];
causes data to contain an array of 2 elements, each of which is an array of 5 elements, each of which is a String
data.lenght is 2, the number of elements in data: {data[0], data[1]}
data[0].lengh is 5, the number of elements in data[0] : {data[0][0], data[0][1], data[0][2], data[0][3], data[0][4]}
which is the same as
data[1].lengh, the number of elements in data[1]: {data[1][0], data[1][1], data[1][2], data[1][3], data[1][4]}
Author Commented:
data.lenght is 2, the number of elements in data: {data[0], data[1]}
data.length is somewhat synonymus to number of rows right?

data[1].lengh,
synonymus to y axis
data[0].lengh is  same as data[1].lengh both 5 which makes sense now
Commented:
If rows is what you are calling Dimension 1, then yes.
Commented:
I'm gonna leave this question, as we have covered the points to overexhaustion, and repeating them over and over again is not going to shed any more light on any of it. Good luck.
Author Commented:
data.length) resulted 2
data[0].length) resulted 5

When dealing with 2Dimentional(2D) arrays even for length they should have used two separate  set of brackets(first set[] and second set [] next to data) to be more clear and consistent similar to how they are getting individual elements.
like
data[][].length(may be there is way which i am not aware??)

Commented:
the elements of data are {data[0], data[1]}
You can get the number of elements in data with
System.out.println("Dimension 1: " + data.length);

you can get the data[0] element of data with
String[] row0=data[0];
The elements of data[0] are {data[0][0], data[0][1], data[0][2],data[0][3], data[0][4]}
You can get the length of data[0] with
System.out.println("Dimension 2: " + data[0].length);
which seems clear, consistent and similar to
System.out.println("row0: " + row0.length);

you can get the data[1] element of data with
String[] row1=data[1];
the elements of data[1] are {data[1][0], data[1][1], data[1][2],data[1][3], data[1][4]}
You can get the  length of data[1] with
System.out.println("Dimension 2: " + data[1].length);
which seems clear, consistent and similar to
System.out.println("row1: " + row1.length);

You can get an element of that row with
String data14=data[1][4];
or
String data14=row1[4];

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Problem Description:
How to determine the upper bound of a two dimensional array ?

Solution:
Following example helps to determine the upper bound of a two dimensional array with the use of arrayname.length.

public class Main {
public static void main(String args[]) {
String[][] data = new String[2][5];
System.out.println("Dimension 1: " + data.length);
System.out.println("Dimension 2: " + data[0].length);
}
}
Result:
The above code sample will produce the following result.

Dimension 1: 2
Dimension 2: 5

The link only talking about symmentric case right where 2D array has symmetric structure of elements(not non symmetric non happy structure)

the elements of data are {data[0], data[1]}
You can get the number of elements in data with
System.out.println("Dimension 1: " + data.length);

you can get the data[0] element of data with
String[] row0=data[0];
The elements of data[0] are {data[0][0], data[0][1], data[0][2],data[0][3], data[0][4]}
You can get the length of data[0] with
System.out.println("Dimension 2: " + data[0].length);
which seems clear, consistent and similar to
System.out.println("row0: " + row0.length);

you can get the data[1] element of data with
String[] row1=data[1];
the elements of data[1] are {data[1][0], data[1][1], data[1][2],data[1][3], data[1][4]}
You can get the  length of data[1] with
System.out.println("Dimension 2: " + data[1].length);
which seems clear, consistent and similar to
System.out.println("row1: " + row1.length);
They should have called
Dimension (without 0 or 1 as it is top level not at sub level)for data.length
then
Dimension 0 for data[0].length
then Dimension 1 for data[1].length

IT Business Systems Analyst / Software DeveloperCommented:
The link only talking about symmentric case right where 2D array has symmetric structure
Yes, you are correct. Java has something of a shortcut "new String[2][5]" that creates the an array to hold 2 subarrays where each of those subarrays holds 5 Strings.

They should have called
Dimension (without 0 or 1 as it is top level not at sub level)for data.length
then
Dimension 0 for data[0].length
then Dimension 1 for data[1].length
No, definitely not. You're still not quite getting it. Maybe it is in the words that they are using. They make mention of Dimension 1 and Dimension 2 simple because the example is for a 2 dimensional array. You could replace the word "Dimension 1" with "Row" and similarly you could replace "Dimension 2" with "Column".

And so...

"data.length" gives you the number or rows (in this case there are 2 rows)

"data[0]" refers to the array of String that make up the first row, or row with index 0

"data[0].length" therefore returns you the number of columns in that first row (in this case there are 5 columns)

"data[1]" refers to the array of String that make up the second row, or row with index 1

"data[1].length" therefore returns you the number of columns in that second row (in this case there are 5 columns, the same as the first row)
Commented:
That would be a somewhat different use of the word "Dimension" than they were using, though perhaps it would have been more clear if they had said something like "extent of dimension 1".
And in the asymmetric case, where data[0].length and data[1].length are different, perhaps the "extent" of a dimension should be the max of any lengh at that level.

But if you use "Dimension 0" and "Dimension 1" to refer to the distinction between  data[0].length and  data[1].length
then what would you call data.length?

.
Author Commented:
"data.length" gives you the number or rows (in this case there are 2 rows)

"data[0]" refers to the array of String that make up the first row, or row with index 0

"data[0].length" therefore returns you the number of columns in that first row (in this case there are 5 columns)

"data[1]" refers to the array of String that make up the second row, or row with index 1

"data[1].length" therefore returns you the number of columns in that second row (in this case there are 5 columns, the same as the first row)

I see above explanation is making it clear to me.
Author Commented:
then what would you call data.length?
number of rows here it is 2 that is also clear.

Now let us focus on the lenght of column elements

1 5 8 9
1 6
6

In above example how do i get like 4 (number of columns)
then 3(number of elemnts in column 1 or column with index 0..not sure if i can say index for columns)
then 2(number of elemnts in column 2 or column with index 1..not sure if i can say index for columns)
then 1(number of elemnts in column 3 or column with index 2..not sure if i can say index for columns)
then 1(number of elemnts in column 4 or column with index 3..not sure if i can say index for columns)

Commented:
If that's what you want to do, then you are better off declaring
String[][] data = {
{"1", "1", "6"},
{"5", "6"},
{"8"},
{"9"},
};
so that you can do
System.out.println("data[0].length is " + data[0].length);
System.out.println("data[1].length is " + data[1].length);
System.out.println("data[2].length is " + data[2].length);
System.out.println("data[3].length is " + data[3].length);

But if you declare
String[][] data = {
{"1", "5", "8", "9"},
{"1", "6"},
{"6"}
};
then you may have to resort to something like
int maxrow=0;
for( String[] row:data ){
maxrow= Math.max(maxrow,row.length);
}
int cols[]=new int [maxrow];
for( String[] row:data ){
for( int c=0;c<row.length;c++ ){
cols[c]++;
}
}
for( int c:cols ){
System.out.println(c);
}
Author Commented:
If that's what you want to do, then you are better off declaring
String[][] data = {
{"1", "1", "6"},
{"5", "6"},
{"8"},
{"9"},
};
so that you can do
System.out.println("data[0].length is " + data[0].length);
System.out.println("data[1].length is " + data[1].length);
System.out.println("data[2].length is " + data[2].length);
System.out.println("data[3].length is " + data[3].length);

Above is clean happy path.

``````But if you declare
String[][] data = {
{"1", "5", "8", "9"},
{"1", "6"},
{"6"}
};
then you may have to resort to something like
int maxrow=0;
for( String[] row:data ){
maxrow= Math.max(maxrow,row.length);
}
int cols[]=new int [maxrow];
for( String[] row:data ){
for( int c=0;c<row.length;c++ ){
cols[c]++;
}
}
for( int c:cols ){
System.out.println(c);
}
``````

Above code is not that clear to me. I have to read and re read few times to understand
Author Commented:
``````maxrow= Math.max(maxrow,row.length);
``````

what is importnace of above step.

we could have said as below right?
maxrow= row.length;
Commented:
You could when row.length >= maxrow, but not when row.length < maxrow
Author Commented:
``````You could when row.length >= maxrow, but not when row.length < maxrow
``````

When is the possibility of getting negative string length?
we defined maxrow as below right

maxrow=o

Commented:
There is not a possibility of getting negative string length.
But there is a possibility of getting non negative maxrow.
Author Commented:
But there is a possibility of getting non negative maxrow.

in the below code we are not getting maxrow right. we just declared it as 0. Please advise

``````But if you declare
String[][] data = {
{"1", "5", "8", "9"},
{"1", "6"},
{"6"}
};
then you may have to resort to something like
int maxrow=0;
for( String[] row:data ){
maxrow= Math.max(maxrow,row.length);
}
int cols[]=new int [maxrow];
for( String[] row:data ){
for( int c=0;c<row.length;c++ ){
cols[c]++;
}
}
for( int c:cols ){
System.out.println(c);
}
``````
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.