Limiting the object Types in a Variable Argument List : Varargs « Class Definition « Java Tutorial






public class MainClass {
  public static void main(String[] args) {
    System.out.println(average(1.0, 2.0, 3.0, 4.0, 5.0));
    System.out.println(average(3.14, 1.414, 1.732));
    System.out.println(average(new Double(7), new Double(8), new Double(9), new Double(10)));
  }
  // Average of a variable number of values
  public static double average(Double... args) {
    if (args.length == 0) {
      return 0.0;
    }
    double ave = 0.0;
    for (double value : args) {
      ave += value;
    }
    return ave / args.length;
  }
}
3.0
2.0953333333333335
8.5








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