Use an array to pass a variable number of arguments to a method. This is the old-style approach to variable-length arguments. : Method Parameters « Class Definition « Java Tutorial






class PassArray {
  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[]) {
    int n1[] = { 10 };
    int n2[] = { 1, 2, 3 };
    int n3[] = {};

    vaTest(n1); // 1 arg
    vaTest(n2); // 3 args
    vaTest(n3); // no args
  }
}








5.7.Method Parameters
5.7.1.By Value or By Reference
5.7.2.Reference Passing Test
5.7.3.Passing objects to methods
5.7.4.Use an array to pass a variable number of arguments to a method. This is the old-style approach to variable-length arguments.