package com.wkcto.chapter03.demo01;
/**
* 可變長參數
* 1) 可變長參數用來接收任意個數據
* 2) 定義可變長參數
* 方法名( 參數類型 參數名, 參數類型 ... 可變長參數名)
* 說明:
* 1) 可變長參數最多只能有一個
* 2) 方法參數列表如果有多個參數, 可變長參數只能放在參數列表的最后
* 3) 在方法體中, 可以把可變長參數當作數組使用
* 4) 在調用方法時,可以傳遞任意個數, 也可以傳遞一個數組
*
* @author 蛙課網
*
*/
public class Test05 {
public static void main(String[] args) {
//在調用方法時,可以傳遞任意個數, 也可以傳遞一個數組
sum();
sum(1);
sum(1,2,3,4,5);
int [] data = {6,6,6,6};
sum(data);
}
//定義方法, 打印任意個整數的和
public static void sum(int ... num ) {
int result = 0;
// 可以把可變長參數當作數組使用
for( int i = 0 ; i<num.length; i++){
result += num[i];
}
System.out.println("sum==" + result);
}
}