[Java] Java中的变长参数的使用

1 基本使用方法

本部分转载自http://www.deitel.com/articles/java_tutorials/20060106/VariableLengthArgumentLists.html

Variable-length argument lists are a new feature in J2SE 5.0. Programmers can create methods that receive an unspecified number of arguments. An argument type followed by an ellipsis (…) in a method’s parameter list indicates that the method receives a variable number of arguments of that particular type. This use of the ellipsis can occur only once in a parameter list, and the ellipsis, together with its type, must be placed at the end of the parameter list. While programmers can use method overloading and array passing to accomplish much of what is accomplished with “varargs,” or variable-length argument lists, using an ellipsis in a method’s parameter list is more concise.

Code below demonstrates method average (lines 7-16), which receives a variable-length sequence of doubles. Java treats the variable-length argument list as an array whose elements are all of the same type. Hence, the method body can manipulate the parameter numbers as an array of doubles. Lines 12-13 use the enhanced for loop to walk through the array and calculate the total of the doubles in the array. Line 15 accesses numbers.length to obtain the size of the numbers array for use in the averaging calculation. Lines 29, 31 and 33 in main call method average with two, three and four arguments, respectively. Method average has a variable-length argument list, so it can average as many double arguments as the caller passes. The output reveals that each call to method average returns the correct value.

// Fig. 7.20: VarargsTest.java
// Using variable-length argument lists.
public class VarargsTest {
    // calculate average
    public static double average( double... numbers ){
        double total = 0.0; // initialize total
        // calculate total using the enhanced for statement
        for ( double d : numbers ){              
            total += d;                          
            return total / numbers.length;
        }
    } // end method average

    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 ) );
    } // end main
} // end class VarargsTest
d1 = 10.0
d2 = 20.0
d3 = 30.0
d4 = 40.0

Average of d1 and d2 is 15.0
Average of d1, d2 and d3 is 20.0
Average of d1, d2, d3 and d4 is 25.0

2 解释与理解

变长参数由编译器编译为数组, 所以使用的时候可以按照像使用数组一样进行遍历。但是如果给定某个含有变长参数的方法,会有如下特例:

someMethod(String... strings)
  • 如果调用上面方法时候参数是null,即: someMethod(null), 那么在方法体内,strings的值为null
  • 如果调用上面方法的参数是多个null, 即: someMethod(null,null), 那么方法体内, strings的类型是 class [Ljava.lang.String;, 即String类型的一维数组,其值为[null, null]
  • 如果不使用参数调用此方法,即someMethod(),那么strings在方法体内是含有空字符串的字符数组。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值