Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing List<String> to String... parameter

I'm struggling to pass a List of Strings into a method requiring the parameter "String...".

Can anybody help me out?

// How to put names into dummyMethod?
List<String> names = getNames();

 public void dummyMethod(String... parameter) {
    mInnerList.addAll(Arrays.asList(parameter));
}
like image 213
RuNaWaY87 Avatar asked Aug 26 '15 09:08

RuNaWaY87


2 Answers

You'll have to convert the List<String> to a String array in order to use it in the 'varargs' parameter of dummyMethod. You can use toArray with an extra array as parameter. Otherwise, the method returns an Object[] and it won't compile:

List<String> names = getNames();
dummyMethod(names.toArray(new String[names.size()]));
like image 152
Glorfindel Avatar answered Sep 21 '22 09:09

Glorfindel


You can do the following :

dummyMethod(names.toArray(new String[names.size()]) 

this will convert the list to array

like image 36
Ahmad Al-Kurdi Avatar answered Sep 21 '22 09:09

Ahmad Al-Kurdi