可变参数

可变参数

一定要实践

  1. JDK1.5开始,java支持传递同类型的可变参数给一个方法
  2. 在方法声明中,在指定参数类型后面增加一个省略号...
  3. 一个方法中只能指定一个可变参数,并且必须是方法的最后一个参数。任何普通参数必须在它之前

代码

package com.example.demo_kuang.method;

public class CommandLine {
    public static void main(String[] args) {
        test(1, 3, 5);
    }


    //可变长参数
    public static void test(double... i) {
        if (i.length == 0) {
            System.out.println("no input");
            return;
        }

        double result = i[0];
        for (int j = 1; j < i.length; j++) {
            if (j > result) {
                result = j;
            }
        }
        System.out.println(result);

    }
}

输出

2.0
posted @ 2021-10-27 17:34  Oh,mydream!  阅读(27)  评论(0编辑  收藏  举报