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));
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With