How can I create a Java method that accepts a variable number of arguments?

前端 未结 7 698
野的像风
野的像风 2020-11-28 07:51

For example, Java\'s own String.format() supports a variable number of arguments.

String.format(\"Hello %s! ABC %d!\", \"World\", 123);
//=>          


        
7条回答
  •  情深已故
    2020-11-28 08:15

    You can pass all similar type values in the function while calling it. In the function definition put a array so that all the passed values can be collected in that array. e.g. .

    static void demo (String ... stringArray) {
      your code goes here where read the array stringArray
    }
    

提交回复
热议问题