可变参数

 

 

 1 package method;
 2 
 3 public class Demo04 {
 4     public static void main(String[] args) {
 5         Demo04 demo04 = new Demo04();
 6         demo04.test(1,2,3,4,5,6,7);
 7     }
 8     //test方法
 9     public void test(int...i){
10         System.out.println(i[0]);//1
11         System.out.println(i[1]);//2
12         System.out.println(i[2]);//3
13         System.out.println(i[3]);//4
14         System.out.println(i[4]);//5
15         System.out.println(i[5]);//6
16         System.out.println(i[6]);//7
17     }
18 }
 1 package method;
 2 
 3 public class Demo05 {
 4     public static void main(String[] args) {
 5 
 6         printMax(5,6,88,77,66);//88
 7         printMax(new double[]{1,2,3});//3
 8     }
 9     public static void printMax(double ...numbers){
10         if (numbers.length == 0){
11             System.out.println("no argument passed");
12             return;
13         }
14         double result = numbers[0];
15         //排序
16         for (int i = 0; i < numbers.length; i++) {
17             if (numbers[i] > result){
18                 result = numbers[i];
19             }
20         }
21         System.out.println("The max value is " + result);
22     }
23 }

 

posted on 2022-07-23 08:59  一枚努力学习的小白  阅读(16)  评论(0编辑  收藏  举报