0

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[])?

1
  • 6
    by simply calling it Commented Aug 13, 2012 at 8:56

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

3

A String varargs, is a String array. You just simply call it and pass the String array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.