Section 7.7 Enhanced for Statement

• The enhanced for statement (p. 262) allows you to iterate through the elements of an array or a collection without using a counter. The syntax of an enhanced for statement is:

for (parameter : arrayName)   statement

where parameter has a type and an identifier (e.g., int number), and arrayName is the array through which to iterate.

• The enhanced for statement cannot be used to modify elements in an array. If a program needs to modify elements, use the traditional counter-controlled for statement.

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.