/*
** 可变参数案例
*/
public class changeVar {
public static void main(String[] args) {
printMax(34, 3, 2, 1);
printMax(new double[]{1, 2, 31, 42});
}
public void test(int x, int... i) {
//可变参数只能放在参数最后面
}
//排序算法,取最大值
public static void printMax(double... numbers) {
if (numbers.length == 0) {
System.out.println("No argument passed.");
return;
}
double result = numbers[0];
//排序
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] > result) {
result = numbers[i];
}
}
System.out.println("The max value is " + result);
}
}
本文来自博客园,作者:南北极星,转载请注明原文链接:https://www.cnblogs.com/jcliaoyb/p/17810678.html