可变参数
public void test(int…i):不定项传递
package com.xiyue.method;
public class Demo04 {
public static void main(String[] args) {
Demo04 demo04 = new Demo04();// demo04就是对象
demo04.test(1,2,3,5,6,8,5);
}
public void test(int...i){
System.out.println(i[0]);
}
}
练习:输入34,3,2,56.5比大小; 1,2,3比大小
package com.xiyue.method;
public class Demo05 {
public static void main(String[] args) {
//调用可变参数的方法
printMax(34,3,2,56.5);
printMax(new double[]{1,2,3});//
}
public static void printMax(double...numbers){
if(numbers.length==0){ //判断传递参数
System.out.println("No argument passed");
return;
}
double result=numbers[0];
//排序
for (int i = 1; i <numbers.length ; i++) {
if (numbers[i]>result){
result=numbers[i];
}
}
System.out.println("The max value is "+result);
}
}
知识点: