Java Reference
In-Depth Information
Consider the following method definition:
public static double
largest(
double
... list)
{
double
max;
if
(list.length != 0)
{
max = list[0];
for
(
int
index = 1; index < list.length; index++)
{
if
(max < list[index])
max = list[index];
}
return
max;
}
return
0.0;
}
The formal parameter
list
of the method
largest
is of variable length. In a call to
the method
largest
, you can specify either any number of actual parameters of type
double
or an array of type
double
. If the actual parameters of the method
largest
are of type
double
, then the values of the actual parameters are put in the array
list
. Because the number of actual parameters can be zero, in which case the length
of
list
is
0
, before determining the largest number in
list
we check whether the
length of
list
is
0
.
Consider the following statements:
double
num1 = largest(34, 56);
//Line 1
double
num2 = largest(12.56, 84, 92);
//Line 2
double
num3 = largest(98.32, 77, 64.67, 56);
//Line 3
System.out.println(largest(22.50, 67.78,
92.58, 45, 34, 56));
//Line 4
double
[] numberList = {18.50, 44, 56.23, 17.89,
92.34, 112.0, 77, 11, 22,
86.62);
//Line 5
System.out.println(largest(numberList));
//Line 6
In Line 1, the method
largest
is called with two parameters; in Line 2 it is called with
three parameters; in Line 3 it is called with four parameters; and in Line 4 it is called with
six parameters. In Line 6, the actual parameter of the method
largest
is the array
numberList
.
Search WWH ::
Custom Search