The solution to the indeterminate number of method parameters in Java, one is to use overloading, the other is to use variable long parameter list to solve, 2 are complementary relationship.
Here we mainly talk about the implementation of the second method:
module sixth;
public class Varargsdemo {
public static void Main (string[] args) {
TODO auto-generated Method Stub
Printmax (34,3,3,2,56.5);
Printmax (New double[]{1,2,3});
}
public static void Printmax (double ... number)
{
if (Number.length = = 0)
{
System.out.println ("No argument passed");
return;
}
Double result = number[0];
for (int i = 1; i < number.length;i++)
{
if (Number[i] > result)
result = Number[i];
}
System.out.println ("The max value is" + result);
}
}
This method of transfer, as long as the parameters of the same type can be used, it does not specify only a few parameters, it will all the arguments placed in an array (so the data types are the same), so as long as the array to do processing, is the processing of these parameters.
The argument number of the method is discussed.