Arrays and Variable Length Parameters List
Submitted by GeePee on Wednesday, May 27, 2015 - 22:25.
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.
Sample Run:
is called with two parameters;
Three parameters
Four parameters
And nine parameters.
The statement the method largest is called using an array of numbers.
The statement is called no parameters.
- import java.util.*;
- public class LargestNumber
- {
- {
- double[] numberList = {23, 45.5, 89,34, 92.78, 36, 90, 120.89,
- 97, 23, 90, 89};
- + largest(5.6, 10.8));
- + largest(23,78, 56));
- + largest(93, 28, 83, 66));
- + "\n"
- + "98.45, 25, 78, 23 and 36 is: "
- + largest(22.5, 12.34, 56.34, 78, 98.45, 25, 78, 23, 36));
- + largest(numberList));
- + " 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;
- }
- }
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 - + largest(5.6, 10.8));
- + largest(23,78, 56));
- + largest(93, 28, 83, 66));
- + "\n"
- + "98.45, 25, 78, 23 and 36 is: "
- + largest(22.5, 12.34, 56.34, 78, 98.45, 25, 78, 23, 36));
- + largest(numberList));
- + " parameter list returns the value " + largest());
Add new comment
- 71 views