Methods Accepting a Variable Number of objects : Varargs « Class Definition « Java Tutorial






public class MainClass {
  public static void main(String[] args) {
    printAll(2, "two", 4, "four", 4.5, "four point five"); 
    printAll();
    printAll(25, "Anything goes", true, 4E4, false);
  }
  public static void printAll(Object... args) {
    for (Object arg : args) {
      System.out.print("  " + arg);
    }
    System.out.println();
  }
}
2  two  4  four  4.5  four point five
25  Anything goes  true  40000.0  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