Variable parameter elaboration is supported in Java

  • 2020-04-01 03:51:10
  • OfStack

The number of parameters can be written as needed, you can write 1, 2, 3,,,,,, they are saved into an array of parameters.

But these arguments have some constraints: they must be of the same type, such as a String String.

At the same time, there are constraints on how arguments are written in variable-argument functions: for example, an array of variable-argument arguments must be written at the end of the argument, or the program will not know how many arguments you have.

Example: output parameter values in a mutable parameter


public class VariableArgument {
  public static void main(String[] args) {
    printArgumentsInfo("aaa","bbb","ccc","ddd","eee");
  }
  
  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

The above is all the content of this article, I hope you can enjoy it.


Related articles: