Getting the length of the array in Java is simple: the array name. Length, let's see a simple program on it, as follows:
This is the last time that the program, I just changed for the back of the 4 into a a.length, perhaps someone asked, then why do not require the length of the group? When the array starts, is the length determined? Yes, but sometimes it happens that we don't know the length of the array, for example, when a function parameter is an array (we'll talk about the function later), for example, if the length of the array is entered by the user? Now let's do a program that lets the user enter an integer data as the length of the array, and then create an array of that length, randomly assigning values to the elements inside, and finally printing the array, and the program is as follows:
Of course, some people may say, since we have got the size in the program, then simply use size, it is not necessary to use length it? Well, I can only tell you that this is true for this program. However, the size can be changed, it does not always represent the current length of the array, but the length is definitely the size of this array. In addition, for (int element:array) ..., here is the For Each loop, which belongs to the for loop category, but we did not say that because the For Each loop involves an array, we have to say the array before we can say the For Each loop.
Okay, I'm going to be here today, I got a headache, I'll go get some sleep.
Java Beginner: Array, get array length