When to prefer a varargs list to an array?

后端 未结 4 2004
无人共我
无人共我 2020-12-19 00:34

I\'m implementing an API an have a method which you pass a list of paths where the program reads resources from

public void importFrom(String... paths) {

}
         


        
4条回答
  •  梦毁少年i
    2020-12-19 00:59

    Since varargs arguments get compiled into a single array argument you could generally prefer varargs since this might be more convinient in some cases and still allows to pass an array in other cases.

    public void importFrom(String... paths)
    {
    }
    

    compiles into

    public void importFrom(String[] paths)
    {
    }
    

    Alternatively you could also use Iterable to make it easier to pass the arguments as collection.

提交回复
热议问题