views:

332

answers:

4

Can you tell me if I am correct in this question? It's a homework question, I don't want the answer. I just want to make sure that I am correct.

It is possible to declare a method that will allow a variable number of parameters.
The symbolism used in the definition that indicate that the method should allow a variable number of parameters is _

Answer: varargs

Thanks

+4  A: 

The wording of the question is somewhat ambiguous. While varargs is the technical name for it, I'm inclined to think that the question is actually asking what syntax is used in order to indicate a varargs parameter.

Anon.
Perhaps I should add as an addendum that simply putting "..." as your answer might not get the result you hope for.
Anon.
+1  A: 

That's correct. You can find more about it in this Sun guide.

Here's an example:

void foo(String... args) {
    for (String arg : args) {
        System.out.println(arg);
    }
}

which can be called as

foo("foo"); // Single arg.
foo("foo", "bar"); // Multiple args.
foo("foo", "bar", "lol"); // Don't matter how many!
foo(new String[] { "foo", "bar" }); // Arrays are also accepted.
BalusC
"It's a homework question, I don't want the answer."
S.Lott
I just confirmed the doubt and gave an example.
BalusC
+1  A: 

Yup...since Java 5: http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html

Roland Bouman
+1  A: 

Yes, it's possible:

public void myMethod(int...numbers) { ... }
Dolph