Java array initialization within argument list

前端 未结 4 1529
野趣味
野趣味 2020-12-11 15:21

How come the first call to someMethod doesn\'t compile without being explicit that it\'s String[]?

It\'s fine to use an array initializer to create a String[] arra

4条回答
  •  温柔的废话
    2020-12-11 15:54

    If you don't want to use explicit String[], use:

    public void someMethod(String... arr){
        //do some magic
    }
    …
    someMethod("cm", "applicant", "lead");
    

    The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments.

    Read more.

提交回复
热议问题