Arrays and Variable Length Parameters List
The following Java program application uses Arrays and Variable Length Parameters List. I will be using the JCreator IDE in developing the program.
To start in this tutorial, first open the JCreator IDE, click new and paste the following code.
import java.util.*;
public class LargestNumber
{
public static void main
(String[] args
) {
double[] numberList = {23, 45.5, 89,34, 92.78, 36, 90, 120.89,
97, 23, 90, 89};
System.
out.
println("The larger 0f 5.6 and 10.8 is: " + largest(5.6, 10.8));
System.
out.
println("The largest of 23, 78, and 56 is: " + largest(23,78, 56));
System.
out.
println("The largest number of 93, 28, 83 and 66 is: " + largest(93, 28, 83, 66));
System.
out.
println("The largest of 22.5 , 12.34, 56.34, 78, " + "\n"
+ "98.45, 25, 78, 23 and 36 is: "
+ largest(22.5, 12.34, 56.34, 78, 98.45, 25, 78, 23, 36));
System.
out.
println("The largest number in numList is: " + largest(numberList));
System.
out.
println("A call to the method largest with empty \n" + " parameter list returns the value " + largest());
}
public static double largest(double ... numList)
{
double max;
int index;
if (numList.length != 0)
{
max = numList[0];
for (index = 1; index < numList.length; index++)
{
if (max < numList [index])
max = numList [index];
}
return max;
}
return 0.0;
}
}
Sample Run:
The larger 0f 5.6 and 10.8 is: 10.8
The largest of 23, 78, and 56 is: 78.0
The largest number of 93, 28, 83 and 66 is: 93.0
The largest of 22.5 , 12.34, 56.34, 78,
98.45, 25, 78, 23 and 36 is: 98.45
The largest number in numList is: 120.89
A call to the method largest with empty
parameter list returns the value 0.0
The preceding program works as follows:
The method
System.
out.
println("The larger 0f 5.6 and 10.8 is: " + largest(5.6, 10.8));
is called with two parameters;
Three parameters
System.
out.
println("The largest of 23, 78, and 56 is: " + largest(23,78, 56));
Four parameters
System.
out.
println("The largest number of 93, 28, 83 and 66 is: " + largest(93, 28, 83, 66));
And
System.
out.
println("The largest of 22.5 , 12.34, 56.34, 78, " + "\n"
+ "98.45, 25, 78, 23 and 36 is: "
+ largest(22.5, 12.34, 56.34, 78, 98.45, 25, 78, 23, 36));
nine parameters.
The statement
System.
out.
println("The largest number in numList is: " + largest(numberList));
the method largest is called using an array of numbers.
The statement
System.
out.
println("A call to the method largest with empty \n" + " parameter list returns the value " + largest());
is called no parameters.