For each is a newly added loop structure of JDK. It can be used to process each element in the array (other types of element sets can also be used) without being distracted by specifying subscripts.
The format is as follows:
For (variable: Collection) Statement
Define a variable to save each element in the set and execute the corresponding statement (of course, it can also be a statement block ). The Set expression must be an array or a class that implements the lterable interface (for example, arraylist.
Example: public class clstest {
Public static void main (string [] ARGs ){
Int [] A = new int [10];
For (int B: ){
System. Out. println (B );
}}}
The following for loop can also achieve the same effect
For (INT I = 0; I <A. length; I ++ ){
Sytem. Out. println (A [I]);
}
The advantage of using the for each loop statement is that it is more concise and error-prone, so you don't have to worry about the starting value of the lower mark and the river termination value.