Enhanced for (PART1:PART2) {part3};
Part2 is an array object, or a collection with a generic nature.
Part1 defines a local variable that is the same type as the object element in Part2.
Part3, of course, is the loop body.
The
foreach statement is a special simplified version of the for statement, but the foreach statement does not completely replace the For statement, however, any foreach statement can be rewritten as a version of the for statement.
foreach is not a keyword, and it is customary to call this special for-statement format a "foreach" statement. To understand the meaning of foreach as "for each" from the literal meaning of English. That's actually what this means.
foreach statement format:for
(element type t element variable x: Traverse object obj) {
reference x Java statement;
The
foreach statement is an enhanced version of the For statement in special cases, simplifying programming, and improving the readability and security of the code (without fear of array out-of-bounds). This is a good addition to the old for statement. If you want to use foreach, don't use for anymore. In the case of a set or array index, foreach appears to be powerless, and this is the time to use a for statement. The Foreach loop can only be used for reading and cannot change the corresponding value
foreach usage and summary in Java