请告知我如何转换以下程序进入可变参数是由Java 5中引入的新功能,仪式现在我利用匿名内部数组..关于变参数和匿名阵列
public class AnnonymousArrayExample {
public static void main(String[] args) {
//calling method with anonymous array argument
System.out.println("first total of numbers: " + sum(new int[]{ 1, 2,3,4}));
System.out.println("second total of numbers: " + sum(new int[]{ 1, 2,3,4,5,6,}));
}
//method which takes an array as argument
public static int sum(int[] numbers){
int total = 0;
for(int i: numbers){
total = total + i;
}
return total;
}
}
调用部分还需要做些什么改变,请指教。 – user2045633 2013-02-15 06:42:59
调用部分没有变化 – 2013-02-15 06:43:17
你可以称它为sum(1,2,3,4); //任何数量的参数 – 2013-02-15 06:46:28