Mega Code Archive

 
Categories / Java Tutorial / Class Definition
 

Demonstrating variable-length arguments

A variable-length argument is specified by three periods (...). static void yourMethodInVarargs(int ... v) {} yourMethodInVarargs( ) can be called with zero or more arguments. v is implicitly declared as an array of type int[ ]. Inside vaTest( ), v is accessed using the normal array syntax. public class MainClass {   // vaTest() now uses a vararg.   public static void vaTest(int... v) {     System.out.print("Number of args: " + v.length + " Contents: ");     for (int x : v)       System.out.print(x + " ");     System.out.println();   }   public static void main(String args[]) {     vaTest(10); // 1 arg     vaTest(1, 2, 3); // 3 args     vaTest(); // no args   } } Number of args: 1 Contents: 10 Number of args: 3 Contents: 1 2 3 Number of args: 0 Contents: