We might want to do something about the parameters in one method in some particular requirement environment, and the parameters in these methods are not specified, so what do we do? In fact, Java has considered this situation for us, that is, the use of variable parameters
Features of variable parameters:
1: Can only appear at the end of the parameter list
2: ... Between the variable type and the variable name, there are no spaces before and after it can be
3: When you call a variable parameter method, the editor creates an array for the mutable parameter implicitly, accessing the mutable parameter as an array in the method body
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 = A;
for (int i=0;i<x.length;i++) {
Sum+=x[i];
}
return sum;
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java mutable parameters