Source: http://www.imooc.com/code/1531
In practical development we often use loops to control the operation of array members. such as:
Operation Result:
Which is used to get the length of the array
"Minor problems" to be aware of:
1, array subscript starting from 0 . So Scores[3], which represents the 4th element in the array, not the 3rd element
2, the array subscript range is 0 to the array length-1 , if the cross-border access, will be error. Such as:
The runtime will report the following error:
The above error message means that the array subscript exceeds the range, that is, the array access is out of bounds. In the above code, create an array of length 2, so the array subscript range is 0 to 1, and the program subscript appears 2, that is, scores[2], over the range, resulting in array access.
Task
The function of the output of the array element is to complement the code in the blank space after line 9 in the editor.
Operation Result:
1 Public classHelloWorld {2 Public Static voidMain (string[] args) {3 4 //defines a string array of length 3 and assigns the initial value5String[] Hobbys = {"Sports", "Game", "movie" };6System.out.println ("Value of elements in the loop output array:");7 8 //iterating through the elements in an array using loops9 Ten One A - - the - } -}
1 Public classHelloWorld {2 Public Static voidMain (string[] args) {3 4 //defines a string array of length 3 and assigns the initial value5String[] Hobbys = {"Sports", "Game", "movie" };6System.out.println ("Value of elements in the loop output array:");7 8 //iterating through the elements in an array using loops9 for(inti = 0; I < 3; i++) {Ten System.out.println (Hobbys[i]); One } A } -}
Web-android Engineer first-6-5 using loops to manipulate arrays in Java