foreach usage and summary in Java

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.