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

前端 未结 7 701
野的像风
野的像风 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:40

    The variable arguments must be the last of the parameters specified in your function declaration. If you try to specify another parameter after the variable arguments, the compiler will complain since there is no way to determine how many of the parameters actually belong to the variable argument.

    void print(final String format, final String... arguments) {
        System.out.format( format, arguments );
    }
    

提交回复
热议问题