多个参数,不定参数,简单例子
注意:不定参数args需要写在方法最后一个参数
在函数中被当成数组处理,所以可以用容器来达到同样的效果。
简单例子:
public static void main(String[] args) {
System.out.println("和为:"+f(3,2,4,5));
}
//args必须写在函数的最后一个参数
public static int f(int i, int... args){
System.out.println("共"+i+"个参数");
int total = 0;
for (int a:args) {
total += a;
}
return total;
}