Java Reference
In-Depth Information
The enhanced
for
statement can be used in place of the counter-controlled
for
state-
ment whenever code looping through an array does
not
require access to the counter indi-
cating the index of the current array element. For example, totaling the integers in an array
requires access only to the element values—the index of each element is irrelevant. How-
ever, if a program must use a counter for some reason other than simply to loop through
an array (e.g., to print an index number next to each array element value, as in the exam-
ples earlier in this chapter), use the counter-controlled
for
statement.
Error-Prevention Tip 7.2
The enhanced
for
statement simplifies the code for iterating through an array making the
code more readable and eliminating several error possibilities, such as improperly specifying
the control variable's initial value, the loop-continuation test and the increment expression.
Java SE 8
The
for
statement and the enhanced
for
statement each iterate sequentially from a starting
value to an ending value. In Chapter 17, Java SE 8 Lambdas and Streams, you'll learn about
class
Stream
and its
forEach
method. Working together, these provide an elegant, more
concise and less error prone means for iterating through collections so that some of the it-
erations may occur in parallel with others to achieve better multi-core system performance.
This section demonstrates how to pass arrays and individual array elements as arguments
to methods. To pass an array argument to a method, specify the name of the array
without
any brackets
. For example, if array
hourlyTemperatures
is declared as
double
[] hourlyTemperatures =
new double
[
24
];
then the method call
modifyArray(hourlyTemperatures);
passes the reference of array
hourlyTemperatures
to method
modifyArray
. Every array
object “knows” its own length. Thus, when we pass an array object's reference into a meth-
od, we need not pass the array length as an additional argument.
For a method to receive an array reference through a method call, the method's
parameter list must specify an
array parameter
. For example, the method header for
method
modifyArray
might be written as
void
modifyArray(
double
[] b)
indicating that
modifyArray
receives the reference of a
double
array in parameter
b
. The
method call passes array
hourlyTemperature
's reference, so when the called method uses
the array variable
b
, it
refers to
the same array object as
hourlyTemperatures
in the caller.
When an argument to a method is an entire array or an individual array element of a
reference type, the called method receives a
copy
of the reference. However, when an argu-
ment to a method is an individual array element of a primitive type, the called method
receives a copy of the element's
value
. Such primitive values are called
scalars
or
scalar
quantities
. To pass an individual array element to a method, use the indexed name of the
array element as an argument in the method call.