可变参数代码笔记
package com.xzk.cs;
public class kbcs {
public static void main(String[] args) {
System.out.println(sum(1,2,3,4));
System.out.println(sum(1,2,3,4,5,6));
}
public static int sum(int... nums) {
int n = 0;
for(int i = 0;i<nums.length;i++) {
n+=nums[i];
}
return n;
}
}
补充:当参数有列表有多个参数时,可变参数必须放在最后,否则会报错!