How can I call a function which accepts unknown number of String inputs (e.g. void func(String... strs)) with a given String array (String[])?
2 Answers
Varargs arguments are in fact arrays. Given this method:
void func(String... strs);
Calling it is simple (the following are the same):
func("a", "b", "c");
func(new String[] { "a", "b", "c" });
The method implementation can then do things, such as:
void func(String... strs) {
System.out.println("Array length: " + strs.length);
System.out.println("Array content: " + strs[0]);
}
Understand that varargs arguments are just syntactic sugar for call-site convenience.