Solved

# array length example

Posted on 2014-10-30
85 Views
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?
0
Question by:gudii9
• 12
• 11
• 9
• +1

LVL 16

Assisted Solution

krakatoa earned 167 total points
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.
0

LVL 7

Author Comment

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

LVL 84

Expert Comment

data[1][5] is 0
data[1] is {7, 4, 5, 8, 0}
data[1].length is 5
0

LVL 16

Expert Comment

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]
0

LVL 16

Expert Comment

data[1][5] is 0
No! There is no such element as data[1][5]
0

LVL 84

Expert Comment

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
0

LVL 16

Expert Comment

gudii :

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

LVL 16

Expert Comment

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.
**************************************************
0

LVL 84

Expert Comment

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"
0

LVL 16

Expert Comment

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

LVL 84

Expert Comment

Java is unabiguous, English is not unambigous.
0

LVL 16

Expert Comment

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

LVL 7

Author Comment

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
0

LVL 7

Author Comment

data[0][5] is 6 this is my mistake it supposed to be data[0][4] is 6
0

LVL 16

Expert Comment

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

LVL 84

Expert Comment

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]}
0

LVL 7

Author Comment

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
0

LVL 84

Expert Comment

If rows is what you are calling Dimension 1, then yes.
0

LVL 16

Expert Comment

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

LVL 7

Author Comment

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

0

LVL 84

Accepted Solution

ozo earned 167 total points
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];
0

LVL 7

Author Comment

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

0

LVL 35

Assisted Solution

mccarl earned 166 total points
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)
0

LVL 84

Expert Comment

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?

.
0

LVL 7

Author Comment

"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.
0

LVL 7

Author Comment

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)

0

LVL 84

Expert Comment

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);
}
0

LVL 7

Author Comment

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
0

LVL 7

Author Comment

``````maxrow= Math.max(maxrow,row.length);
``````

what is importnace of above step.

we could have said as below right?
maxrow= row.length;
0

LVL 84

Expert Comment

You could when row.length >= maxrow, but not when row.length < maxrow
0

LVL 7

Author Comment

``````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

0

LVL 84

Expert Comment

There is not a possibility of getting negative string length.
But there is a possibility of getting non negative maxrow.
0

LVL 7

Author Comment

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);
}
``````
0

## Featured Post

### Suggested Solutions

If you havenâ€™t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.Â  You will also find the â€¦
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relatâ€¦