Can I pass an array as arguments to a method with variable arguments in Java?

前端 未结 5 1862
我寻月下人不归
我寻月下人不归 2020-11-22 07:52

I\'d like to be able to create a function like:

class A {
  private String extraVar;
  public String myFormat(String format, Object ... args){
    return Str         


        
5条回答
  •  臣服心动
    2020-11-22 08:06

    The underlying type of a variadic method function(Object... args) is function(Object[] args). Sun added varargs in this manner to preserve backwards compatibility.

    So you should just be able to prepend extraVar to args and call String.format(format, args).

提交回复
热议问题