2010年4月22日 星期四

Variable Lenght

1. FUN(Type... Name)
2. caller (TYPE A, TYPE B)
3. REceive
for (TYPE a: Name) // is a array
{
...
}

// Fig. 7.20: VarargsTest.java
2 // Using variable-length argument lists.
3
4 public class VarargsTest
5 {
6 // calculate average
7 public static double average( double... numbers )
8 {
9 double total = 0.0; // initialize total
10
11 // calculate total using the enhanced for statement
12 for ( double d : numbers )
13 total += d;
14
15 return total / numbers.length;
16 } // end method average
17
18 public static void main( String args[] )
19 {
20 double d1 = 10.0;
21 double d2 = 20.0;
22 double d3 = 30.0;
23 double d4 = 40.0;
24
25 System.out.printf( "d1 = %.1f\nd2 = %.1f\nd3 = %.1f\nd4 = %.1f\n\n",
26 d1, d2, d3, d4 );
27
28 System.out.printf( "Average of d1 and d2 is %.1f\n",
29 average( d1, d2 ) );
30 System.out.printf( "Average of d1, d2 and d3 is %.1f\n",
31 average( d1, d2, d3 ) );
32 System.out.printf( "Average of d1, d2, d3 and d4 is %.1f\n",
33 average( d1, d2, d3, d4 ) );
34 } // end main
35 } // end class VarargsTest

=== Reference ===
1. http://www.deitel.com/articles/java_tutorials/20060106/VariableLengthArgumentLists.html

沒有留言:

張貼留言