The expression of the variable parameter list is to add an ellipsis after the data type. E.g,
public void sum(int... n){
}
Variable parameters have the following rules:
- The variable parameter list is used as a parameter, and 0 or more parameters can be passed in when the method is called, and it can also be passed in an array. E.g,
//以下两种引用方式是都可行的
sum(1,2,3);
int[] a = {
1,2,3};
sum(a);
-
However, arrays are used as method parameters, and only arrays can be passed in when the method is called.
-
If there are more than two parameters in the parameter list, the variable parameter must be at the end, and there can only be one variable parameter in a method.
-
In the method definition, it is considered that the variable parameter list and the array are repeatedly defined instead of overloading. E.g,
public void sum(int[] n){
}
public void sum(int... n){
}
These two methods are considered to be defined repeatedly.
- The method where the variable parameter list is located is the last to be accessed. For example,
when the following two methods exist at the same time, the method without a variable list will be accessed first.
public int plus(int a, int b){
} //优先访问
public int plus(int.. a){
}