开发者

identifying .length in two dimensional array

开发者 https://www.devze.com 2023-04-01 08:49 出处:网络
What is the use of .length here? What could be substituted with .length i开发者_高级运维n the code below? I just wanted to understand the code better, Exams tomorrow! W00t!

What is the use of .length here? What could be substituted with .length i开发者_高级运维n the code below? I just wanted to understand the code better, Exams tomorrow! W00t!

public class twoDimension{
  public static void main(String[] args) {
    int[][] a2 = new int[10][5];
    for (int i=0; i<a2.length; i++) {
      for (int j=0; j<a2[i].length; j++) {
        a2[i][j] = i;
        System.out.print(" " + a2[i][j]);
      }
      System.out.println("");
    }
  }
}


You have declared an array of arrays of int type and initialized it.

10 inside the first square bracket says that you are going to store 10 values for the row and is the size of the array ‘n’ where n=10. The row length is 10.

5 inside the second square bracket says that you are going to store 5 values for the column and is the size of the array ‘n’ where n=5. The column length is 5.

In this case, you have one array with 10 locations

streetX[house_1]  
streetX[house_2]    
streetX[house_3]  
streetX[house_4]  
streetX[house_5]  
streetX[house_6]  
streetX[house_7]  
streetX[house_8]  
streetX[house_9]     
streetX[house_10]  

and inside each of the location is another 5 different locations

{house_i[room_number1], house_i[room_number2], house_i[room_number3], house_i[room_number4], house_i[room_number5]} 

where i can represent any of 1 to 10

Think of it as 10 houses on a street. Each of the houses having 5 rooms. Each house will have a unique address to distinguish it from other houses on the street. In the same manner, each room in one house will be different from the rest. You can now keep different stuffs in each room.

The street is has an array of houses and each house has an array of room.

Therefore, you will have: You can think of a2 as streetX, i.e. a2 = streetX

streetX has 10 houses, therefore, there are 10 locations in a2. The size of a2 is 10 meaning that the length is 10. Therefore, a2.length = 10

NOTE: When you refer the array values, the index starts from 0 ‘zero’ to ‘n-1′. An array index is always a whole number and it can be a int, short, byte, or char.

house_i (where i ranges from 1 t0 10) but we usually count the index from 0. We'll have index 0 to 9. house_i is similar to a2[i]. There are 5 rooms, hence 5 locations. The size of any a2[i] (i.e. any house a2[0], a2[1], a2[2], a2[3] or a2[4])) is 5 meaning that the length is 5. Therefore, a2[i].length = 5

 a2[house1][room1]  a2[house1][room2] a2[house1][room3] a2[house1][room4] a2[house1][room5]
 a2[house2][room1]  a2[house2][room2] a2[house2][room3] a2[house2][room4] a2[house2][room5]  
 a2[house3][room1]  a2[house3][room2] a2[house3][room3] a2[house3][room4] a2[house3][room5]  
 a2[house4][room1]  a2[house4][room2] a2[house4][room3] a2[house4][room4] a2[house4][room5]  
 a2[house5][room1]  a2[house5][room2] a2[house5][room3] a2[house5][room4] a2[house5][room5]  
 a2[house6][room1]  a2[house6][room2] a2[house6][room3] a2[house6][room4] a2[house6][room5]
 a2[house7][room1]  a2[house7][room2] a2[house7][room3] a2[house7][room4] a2[house7][room5]
 a2[house8][room1]  a2[house8][room2] a2[house8][room3] a2[house8][room4] a2[house8][room5]
 a2[house9][room1]  a2[house9][room2] a2[house9][room3] a2[house9][room4] a2[house9][room5]
a2[house10][room1] a2[house10][room2] a2[house10][room3] a2[house10][room4] a2[house10[room5]

NOTE: When you refer the array values, the index starts from 0 ‘zero’ to ‘n-1′. An array index is always a whole number and it can be a int, short, byte, or char.

If n = 10, we'll have 0 ... 9
If n = 5, we'll have 0 ... 4

As an example, streetX[house1][room1] = a2[house1[room1] will become a2[0][0]

Eventually, you will have:

a2[0][0] a2[0][1] a2[0][2] a2[0][3] a2[0][4]

a2[1][0] a2[1][1] a2[1][2] a2[1][3] a2[1][4]  

a2[2][0] a2[2][1] a2[2][2] a2[2][3] a2[2][4]   

a2[3][0] a2[3][1] a2[3][2] a2[3][3] a2[3][4]   

a2[4][0] a2[4][1] a2[4][2] a2[4][3] a2[4][4]   

a2[5][0] a2[5][1] a2[5][2] a2[5][3] a2[5][4]  

a2[6][0] a2[6][1] a2[6][2] a2[6][3] a2[6][4]   

a2[7][0] a2[7][1] a2[7][2] a2[7][3] a2[7][4]   

a2[8][0] a2[8][1] a2[8][2] a2[8][3] a2[8][4]   

a2[9][0] a2[9][1] a2[9][2] a2[9][3] a2[9][4] 

The outer for...loop will be repeated 10 times, remember, a2.length = 10 and the inner for ... loop is repeated 5 times, remember, a2[].length = 5

You initialized a2[i][j] = i, therefore for each inner loop, you will have the index of i (the house) will be the stored as the value or number of things in each room for the house.

Hence, when you execute the program, you will get the output or result below printed out.

Count the number of rows and columns and observe that on each line the values are the same.

0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
6 6 6 6 6
7 7 7 7 7
8 8 8 8 8
9 9 9 9 9

For an array you can use [array_object_name].length or specify the size in number. E.g. a2.length or 10, or a2[i].length or 5 in you example program. Where i equals any of 0, 1, 2, 3, 4. Note that length in .length is a static variable ( You can read more about static variables from here http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html and learn about static methods and static classes as well)

In summary, the length is the size of an array

You can learn more about array from here - http://javapapers.com/core-java/java-array/


What is the use of .length here?

no different from one dimensional array: to get number of elements in the array, if you treat two dimensional array as a table, then assuming the first .length is the row length, the second .length is the column length. get it?

What could be substituted with .length in the code below?

What do you see in the declaration (construction to be precise)?


a2.length is the number of rows. In this case the outer loop will iterate 10 times. a2[i].length is the length of one specific row.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号