Java variable parameters
In some specific demand environments, we may need to perform some operations on the parameters in a method, and the parameters in these methods are not specified. What should we do if the problem arises? Java actually considers this situation for us, that is, using variable parameters.
Features of variable parameters:
1: it can only appear at the end of the parameter list
2:... is located between the variable type and the variable name. It can be preceded by spaces.
3: When you call a variable parameter method, the editor implicitly creates an array for the variable parameter and accesses the variable parameter in the method body as an array.
Write a demo.
Public class Demo {
Public static void main (String [] args ){
System. out. println (add (2, 3, 4, 5 ));
}
Static int add (int a, int... x ){
Int sum =;
For (int I = 0; I
Sum + = x [I];
}
Return sum;
}
}