Java1.5 adds new features: variable parameters: For cases where the parameter number is indeterminate and the type is determined, Java handles the mutable parameter as an array.
Characteristics:
1. Can only appear at the end of the parameter list;
2. Ellipsis (...) Between the variable type and the variable name, there are no spaces before and after it can be
3. When invoking a method of a mutable parameter, the compiler creates an array for the mutable parameter implicitly,
Accessing variable parameters as an array in the method body
Examples of procedures:
public class Test {//method for passing in Parameters public void Add (int x,int ... ARR)//variable parameter {int sum=x;//first copy the value of the first parameter to sumfor (int i=0;i<arr.length;++i) {sum+=arr[i];} SYSTEM.OUT.PRINTLN (sum);} public static void Main (string[] args) {//TODO auto-generated method Stubtest tt = new Test () Tt.add (2);//and 2, variable parameter length can be 0 Tt.add (2,3);//And for 5tt.add (2,3,4,5);//And for 14tt.add (2,3,4,5,6,7);}}
mutable parameters in Java