How to convert a List to variable argument parameter java

前端 未结 3 2006
情深已故
情深已故 2021-02-03 17:09

I have a method which takes a variable length string (String...) as parameter. I have a List with me. How can I pass this to the method as argument?

3条回答
  •  萌比男神i
    2021-02-03 17:21

    String ... and String[] are identical If you convert your list to array.

    using

    Foo[] array = list.toArray(new Foo[list.size()]);
    

    or

    Foo[] array = new Foo[list.size()];
    list.toArray(array);
    

    then use that array as String ... argument to function.

提交回复
热议问题