Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

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?

Please advise.

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?

Please advise.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

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.

first dimension's first elementthat should be called like data[1][1]

I am missing something. please advise

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?

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]

data[1][5] is 0should have been

data[1][4] is 0

and

data[0][5] is 6should have been

data[0][4] is 6

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

first dimension's first element

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

I am missing something. please advise

yes- you are still missing something ! What you are missing is, as has been said repeatedly throughout your questions :

data[0][0] is 1What 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"

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.

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.

But below output of the program not clear. please advise

data.length) resulted 2

data[0].length) resulted 5

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.

There's no point entering into an off topicquite 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]}

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

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

Any thoughts. please advise

You can get the number of elements in data with

System.out.println("Dimens

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("Dimens

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("Dimens

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

Your issues matter to us.

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

Start your 7-day free trialProblem 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]}They should have called

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

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

Please advise

The link only talking about symmentric case right where 2D array has symmetric structureYes, 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 calledNo, 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".

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

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)

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?

.

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

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)

Please advise

String[][] data = {

{"1", "1", "6"},

{"5", "6"},

{"8"},

{"9"},

};

so that you can do

System.out.println("data[0

System.out.println("data[1

System.out.println("data[2

System.out.println("data[3

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

}

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

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

what is importnace of above step.

we could have said as below right?

maxrow= row.length;

please advise

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

please advise

But there is a possibility of getting non negative maxrow.

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

Java

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

And so if you changed the code to this :

Open in new window

you would get exactly the same output.