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??
Package Com.myjava;import Java.util.arrays;public-class ForEach {public static void main (string[] args) { //define an integer array , save score information int[] scores = {The "," ",", ","); Sorts the array of arrays classes arrays.sort (scores); Use foreach to iterate through the elements in the output array for (int score:scores) {System.out.println (score);} Use foreach to traverse the output array subscript int[] index = {0,1,2,3,4};for (int i:index) {System.out.println (i);}}}
Using the foreach Action array