Through the study in the first two sections, we know how to create arrays. In this section we continue to learn the use of arrays.
One-dimensional arrays access their elements through index symbols , such as boy[0], boy[1], etc. It should be noted that the index starts from 0. Therefore, if the array has 7 elements, the index will end at 6. If the program uses the following statement:
boy[7]=384.98f;
The program can compile and pass, but an ArrayIndexOutOfBoundsException will occur during runtime, so you must be careful when using arrays to prevent the index from going out of bounds .
Two-dimensional arrays also access their own elements through index symbols , such as a[0][1], a[1][2], etc. It should be noted that the index starts from 0. For example, a two-dimensional array a is declared to be created. :
inta[][]=newint[6][8];
Then the first index changes from 0 to 5, and the second index changes from 0 to 7.
The number of elements in an array is called the length of the array . For a one-dimensional array, the value of "array name.length" is the number of elements in the array; for a two-dimensional array, the value of "array name.length" is the number of one-dimensional arrays it contains.
For example:
floata[]=newfloat[12];intb[][]=newint[3][6];
The value of a.length is 12, while the value of b.length is 3.