The following requirements are first: return the largest of the arguments. However, because the number of arguments is uncertain, it is difficult to use overloaded methods.
Workaround: When defining a method, the format of the formal parameter is defined as follows:
int getmax (int first,int ... VarArgs) {
for (int i:varagus)
....
}
Call:
System.out.println ("max (1) =" +demo.getmax (1) + "");//1
System.out.println ("max (2) =" +demo.getmax (2,1) + "");//2
System.out.println ("max (3) =" +demo.getmax (6,4,5,9,8,7) + "");//9
Description
1, variable-length parameters are actually compiled to convert the array.
2, the variable length parameter can only appear in the formal parameter.
3, if in the overloaded method, some have variable-length parameters, some do not, then priority to call the method without variable length parameters. If you define an int getmax (int first), then this method takes precedence
4, if the overloaded method contains variable length parameters, then the error
5, a method can only have a variable length parameter
Variable length parameter