Array Length

+1 Sumanpal Singh · March 8, 2015
Java Programming Tutorial - 34 - Table for Multi Arrays

i can't understand the the legnth of array in this example

Post a Reply

Replies

Oldest  Newest  Rating
+1 Dol Lod · March 8, 2015
In java, length is a built in part of arrays to tell you how long they are. In this case, length is being used to make sure he has valid access to the given elements in his table. 

He is using row access for each two dimensional array. 

ex. table
1 2 3 4
5 6 7 8
9 10 11 12 
13 14 15 16

When he uses the outer length, he is checking against how many rows he has on the table to make sure he has defined access and  then grabbing the row. 
Then he prints out each column in the row individually by navigating through the columns one at a time until there are no more columns left to print out, going to the next row. 
0 Sumanpal Singh · March 9, 2015
Thanks Arjun , Very well explained .
0 Dol Lod · March 9, 2015
np
0 Nicholas Eason · March 12, 2015
A different example:

You have this sweet stair case that doubles as a multidimensional array(pretty sweet, right?):
//COOL STAIRCASE ARRAY:
int stairs[][] = {
{1},
{1 2},
{1 2 3},
{1 2 3 4},
{1 2 3 4 5}};

stairs.length() will return 5, and it's pretty obvious why. Your staircase has 5 steps!

Now, since it's a staircase, each of the STEPS are a different length. So stairs[2].length() is not the same as stairs[3].length().

KEEP IN MIND: Arrays start with an index of 0

You'll see that stairs[2].length() will return 3

stairs[3].length will return 4
0 Sumanpal Singh · March 13, 2015
Thanks Nicholas ........
  • 1

Java / Android Development

106,991 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator