Java方法05:可变参数

 

复制代码
public class Demo03 {
    public static void main(String[] args) {
        Demo03 demo03 = new Demo03();
        demo03.test(1);//输出1

    }

    public void test(int... i){
        System.out.println(i[0]);
    }
}
复制代码
复制代码
public class Demo03 {
    public static void main(String[] args) {
        Demo03 demo03 = new Demo03();
        demo03.test(1,2,3,4,5);//输出1

    }

    public void test(int... i){
        System.out.println(i[0]);//1
        System.out.println(i[1]);//2
        System.out.println(i[2]);//3
        System.out.println(i[3]);//4
        System.out.println(i[4]);//5
    }
}
复制代码
复制代码
public class Demo04 {
    public static void main(String[] args) {
        //调用可变参数的方法
        printMax(34,3,3,2,56.5);//The max value is34.0
        printMax(new double[]{1,2,3});//The max value is3.0
    }
    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.length > result){
                result = numbers[i];
            }
        }
        System.out.println("The max value is"+result);
    }
复制代码

 

posted @   三口一头居  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示