Overloading Vararg Methods : Varargs « Class Definition « Java Tutorial






class MainClass {
  static void vaTest(int... v) {
    System.out.print("vaTest(int ...): " + "Number of args: " + v.length + " Contents: ");

    for (int x : v)
      System.out.print(x + " ");

    System.out.println();
  }

  static void vaTest(boolean... v) {
    System.out.print("vaTest(boolean ...) " + "Number of args: " + v.length + " Contents: ");

    for (boolean x : v)
      System.out.print(x + " ");

    System.out.println();
  }

  static void vaTest(String msg, int... v) {
    System.out.print("vaTest(String, int ...): " + msg + v.length + " Contents: ");

    for (int x : v)
      System.out.print(x + " ");

    System.out.println();
  }

  public static void main(String args[]) {
    vaTest(1, 2, 3);
    vaTest("Testing: ", 10, 20);
    vaTest(true, false, false);
  }
}








5.9.Varargs
5.9.1.Demonstrating variable-length arguments
5.9.2.Using varargs with standard arguments
5.9.3.Methods Accepting a Variable Number of objects
5.9.4.Limiting the object Types in a Variable Argument List
5.9.5.Demonstrate variable-length arguments.
5.9.6.Use varargs with standard arguments.
5.9.7.Overloading Vararg Methods
5.9.8.Make methods that have unspecified number of parameters:pass an array of Objects