Using variable-length argument lists. - Java Object Oriented Design

Java examples for Object Oriented Design:Method Parameter

Description

Using variable-length argument lists.

Demo Code

public class Main 
{
   // calculate average
   public static double average(double... numbers)
   {/*from w  ww  . j av  a2  s . com*/
      double total = 0.0; 

      // calculate total using the enhanced for statement
      for (double d : numbers)
         total += d;

      return total / numbers.length;
   } 

   public static void main(String[] args) 
   {
      double d1 = 10.0;
      double d2 = 20.0;
      double d3 = 30.0;
      double d4 = 40.0;

      System.out.printf("d1 = %.1f%nd2 = %.1f%nd3 = %.1f%nd4 = %.1f%n%n",
         d1, d2, d3, d4);

      System.out.printf("Average of d1 and d2 is %.1f%n", 
         average(d1, d2)); 
      System.out.printf("Average of d1, d2 and d3 is %.1f%n", 
         average(d1, d2, d3));
      System.out.printf("Average of d1, d2, d3 and d4 is %.1f%n", 
         average(d1, d2, d3, d4));
   } 
}

Result


Related Tutorials