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

前端 未结 7 700
野的像风
野的像风 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 );
    }
    
    0 讨论(0)
提交回复
热议问题