Passing List<String> to String... parameter Passing List<String> to String... parameter arrays arrays

Passing List<String> to String... parameter


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()]));


You can do the following :

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

this will convert the list to array


Pass String array (String[]) inside method. You will have to convert your List to Array and then pass it.

if (names != null) {    dummyMethod(names.toArray(new String[names.size()])); }