1
List<String> strings;
void takeParams(String... params);

How can I pass the list into that a method only taking varargs?

strings.toArray(); is not possible as it returns Object[].

membersound
  • 81,582
  • 193
  • 585
  • 1,120

1 Answers1

4

Use the List#toArray(T[]) method to create an array from the list, and pass it.

takeParams(strings.toArray(new String[strings.size()]));
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • in the case of Redis, it's not working for me. my command is `syncCommands.mget(redisKeys.toArray(new String[redisKeys.size()])); `and I get error msg `[Ljava.lang.String; cannot be cast to java.lang.String.` Any help? – roottraveller Nov 20 '19 at 11:57