Java Reference
In-Depth Information
There is one important point to understand about the for-each style
for
loop. Its iteration
variable is “read-only” as it relates to the underlying array. An assignment to the iteration
variable has no effect on the underlying array. In other words, you can't change the contents
of the array by assigning the iteration variable a new value. For example, consider this pro-
gram:
The first
for
loop increases the value of the iteration variable by a factor of 10. However,
this assignment has no effect on the underlying array
nums
, as the second
for
loop illus-
trates. The output, shown here, proves this point: