Java Reference
In-Depth Information
if
(numList.length != 0)
{
max = numList[0];
for
(
int
index = 1; index < numList.length; index++)
{
if
(max < numList [index])
max = numList [index];
}
return
max;
}
return
0.0;
}
}
Sample Run:
Line 2: The larger of 5.6 and 10.8 is 10.8
Line 3: The largest of 23, 78, and 56 is 78.0
Line 4: The largest of 93, 28, 83, and 66 is 93.0
Line 5: The largest of 22.5, 12.34, 56.34, 78,
98.45, 25, 78, 23 and 36 is 98.45
Line 6: The largest number in numList is 120.89
Line 7: A call to the method largest with an empty
parameter list returns the value 0.0
In the preceding program, in Line 2, the method
largest
is called with two para-
meters; in Line 3 it is called with three parameters; in Line 4 it is called with four
parameters; and in Line 5 it is called with nine parameters. Note that in Line 6, the
method
largest
is called using an array of numbers, but in Line 7 it is called with no
parameters.
Just as you can create a method using the primitive data type as a variable length formal
parameter, you can also create a method with objects as a variable length formal para-
meter (list). Examples 9-11 and 9-12 show you how to do this. First, we specify some
rules to follow when using a variable length formal parameter list.
1. A method can have both a variable length formal parameter and other
formal parameters. For example, consider the following method heading:
public static void
myMethod(String name,
double
num,
int
... intList)
The formal parameter
name
is of type
String
, the formal parameter
num
is of type
double
, and the formal parameter
intList
is of variable
length. The actual parameter corresponding to
intList
can be an
int
array or any number of
int
variables and/or
int
values.
2. A method can have, at most, one variable length formal parameter.
Search WWH ::
Custom Search