5. 9. 2. Using varargs with standard arguments |
|
A method can have "normal' parameters along with a variable-length parameter.
However, the variable-length parameter must be the last parameter. |
There must be only one varargs parameter. |
int aMethod(int a, int b, double c, int ... vals) {}
|
|
int aMethod(int a, int b, double c, int ... vals, double ... morevals) { // Error!
|
|
public class MainClass {
static void vaTest(String msg, int... v) {
System.out.print(msg + v.length + " Contents: ");
for (int x : v) {
System.out.print(x + " ");
}
System.out.println();
}
public static void main(String args[]) {
vaTest("One vararg: ", 10);
vaTest("Three varargs: ", 1, 2, 3);
vaTest("No varargs: ");
}
}
|
|
One vararg: 1 Contents: 10
Three varargs: 3 Contents: 1 2 3
No varargs: 0 Contents: |