Java - What is wrong: variable-length argument?

Question

What is wrong with the following code?

void m2(int...n1, String str) {
   // Code goes here
}


Click to view the answer

The variable-length argument of a varargs method must be the last argument in the argument list.

Note

m2() method is invalid because the variable-length argument n1 is not declared as the last argument:

How to fix

You can fix the above declaration by moving argument n1 to the last, like so:

void m2(String str, int...n1) {
   // Code goes here
}

Related Quiz