Source: http://www.imooc.com/code/1864
foreach is not a keyword in Java, it is a special simplified version of A for statement, which is simpler and easier to iterate through arrays and collections. In terms of the literal meaning of foreach, which means "for each", how do you use a foreach statement?
Grammar:
We use the for and foreach statements to iterate through the array, respectively.
Operation Result:
It's easy to see the foreach!!
Leave a question to everyone: what if you want to get the subscript for an array element in a foreach statement??
Task
An integer array scores is defined in the editor, the student performance information is saved, the result array is sorted by the sort method of the Arrays class, and then the elements in the output array are traversed using foreach. The result of the operation is:
Please complete the code in section 10 and line
1 Importjava.util.Arrays;2 3 Public classHelloWorld {4 5 Public Static voidMain (string[] args) {6 7 //define an integer array and save the score information8 int[] scores = {89, 72, 64, 58, 93 };9 Ten //sorting an array of arrays classes One A - //iterating through the elements in the output array using foreach - for ( ) { the System.out.println (score); - } - } -}
Myans:
1 Importjava.util.Arrays;2 3 Public classHelloWorld {4 5 Public Static voidMain (string[] args) {6 7 //define an integer array and save the score information8 int[] scores = {89, 72, 64, 58, 93 };9 Ten //sorting an array of arrays classes One Arrays.sort (scores); A - //iterating through the elements in the output array using foreach - for(intscore:scores) { the System.out.println (score); - } - } -}
Mu class net-java first season-6-8 using the foreach action array