?
Solved

array length example

Posted on 2014-10-30
33
Medium Priority
?
97 Views
Last Modified: 2014-11-13
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.
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 11
  • 9
  • +1
33 Comments
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 668 total points
ID: 40413244
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);

Open in new window


you would get exactly the same output.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40413681
first dimension's first element
that 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?
0
 
LVL 84

Expert Comment

by:ozo
ID: 40413738
data[1][5] is 0
data[1] is {7, 4, 5, 8, 0}
data[1].length is 5
0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 16

Expert Comment

by:krakatoa
ID: 40413746
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

by:krakatoa
ID: 40413757
data[1][5] is 0
No! There is no such element as data[1][5]
0
 
LVL 84

Expert Comment

by:ozo
ID: 40413759
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

by:krakatoa
ID: 40413789
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

by:krakatoa
ID: 40413826
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.
**************************************************
0
 
LVL 84

Expert Comment

by:ozo
ID: 40413877
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

by:krakatoa
ID: 40413890
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

by:ozo
ID: 40413901
Java is unabiguous, English is not unambigous.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40413914
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

by:gudii9
ID: 40413915
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
0
 
LVL 7

Author Comment

by:gudii9
ID: 40413918
data[0][5] is 6 this is my mistake it supposed to be data[0][4] is 6
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40413955
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

by:ozo
ID: 40413964
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

by:gudii9
ID: 40414276
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

by:ozo
ID: 40414558
If rows is what you are calling Dimension 1, then yes.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40414650
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

by:gudii9
ID: 40419717
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
0
 
LVL 84

Accepted Solution

by:
ozo earned 668 total points
ID: 40420782
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

by:gudii9
ID: 40424369
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

Please advise
0
 
LVL 36

Assisted Solution

by:mccarl
mccarl earned 664 total points
ID: 40425400
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

by:ozo
ID: 40425556
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

by:gudii9
ID: 40426201
"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

by:gudii9
ID: 40426215
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 40426420
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

by:gudii9
ID: 40426463
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);
        }

Open in new window


Above code is not that clear to me. I have to read and re read few times to understand
0
 
LVL 7

Author Comment

by:gudii9
ID: 40435041
maxrow= Math.max(maxrow,row.length);

Open in new window


what is importnace of above step.

we could have said as below right?
maxrow= row.length;
please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40435501
You could when row.length >= maxrow, but not when row.length < maxrow
0
 
LVL 7

Author Comment

by:gudii9
ID: 40435701
You could when row.length >= maxrow, but not when row.length < maxrow

Open in new window


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

maxrow=o

please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40435718
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

by:gudii9
ID: 40435908
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);
        }

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question