gudii9
asked on
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?
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
data[1][5] is 0
data[1] is {7, 4, 5, 8, 0}
data[1].length is 5
data[1] is {7, 4, 5, 8, 0}
data[1].length is 5
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]
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 0No! There is no such element as data[1][5]
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
gudii :
Please refer to my above comment at 16:35:33ID: 40413746 for a correct breakdown of the array's elements.
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 :
**************************
ALL arrays, of ANY type, are indexed from 0 as their first element, to length -1 as the last.
**************************
If that was what was missing, gudii9 would not have said
This is independent of the linguistic ambiguity between "first" and "zeroth"
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"
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
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.
Java is unabiguous, English is not unambigous.
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.
ASKER
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
ASKER
data[0][5] is 6 this is my mistake it supposed to be data[0][4] is 6
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.
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]}
ASKER
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
If rows is what you are calling Dimension 1, then yes.
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.
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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]}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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
.
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?
.
ASKER
"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.
ASKER
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
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);
}
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);
}
ASKER
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
ASKER
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
ASKER
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
There is not a possibility of getting negative string length.
But there is a possibility of getting non negative maxrow.
But there is a possibility of getting non negative maxrow.
ASKER
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);
}
ASKER
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?