Variable parameters are supported in Java
It means: The number of parameters can be written as required, you can write 1, 2, 3 、、、、 They are saved in an array of parameters.
However, these parameters have some constraints: they must be of the same type, such as String type.
At the same time, the arguments in a variable parameter's function are also constrained: for example, an array of mutable parameters must be written at the end of the parameter, or the program will not know how many parameters you have.
Example: output parameter values in a variable parameter
public class Variableargument {
public static void Main (string[] args) {
Printargumentsinfo ("AAA", "BBB", "CCC", "ddd", "Eee");
}
/**
* Printing Parameters
* @param an array of strings parameters
*/
public static void Printargumentsinfo (String...strings) {
for (int i=0;i<strings.length;i++) {
SYSTEM.OUT.PRINTLN ("parameter" + (i+1) + ":" +strings[i]);
}
}
}
Results:
Parameter 1:aaa
Parameter 2:bbb
Parameter 3:CCC
Parameter 4:ddd
Parameter 5:eee
Variable parameters of Java