java:How to use variable parameter? (easy to understand)

Catalogue:

I.Preface :

II.Syntax : 

III.NOTICE(significant) :

IV.Code Demonstrate :

        1.Show notice one, notice two and notice three :

        2.Show notice four : 

        3.Show notice five : 

        4.Exercise:


I.Preface :

        java allows us to encapsulate many methods which have the same name and function but have different number of parameters into one method  at one class.

        We achieve it by using variable parameter in the list of formal parameters.

        By the way, we will use variable parameter in the module java reflect, etc.

II.Syntax : 

        access_modifier return_type method_name (data_structures... parameters) {
    
        }

        ΔIn the list of formal parameters, the format of three points "..." is fix.

III.NOTICE(significant) :

        1. The number of actual parameter can range from 0 to n.(n = 1, 2, 3, 4, 5......)
        2. The actual parameter can be an array directly.
        3. The variable parameter is actually an array, so you can use it as an array.
        4. When variable parameter is used with ordinary parameter, make sure that the             variable parameter is at last of the list of formal parameter.
        5. There could be only one variable parameter at one parameter list.

IV.Code Demonstrate :

        Hey, guys, I am about to demonstrate all the notices mentioned above in code.

        1.Show notice one, notice two and notice three :

package csdn.varparameter;

public class VariableParameter {
    public static void main(String[] args) {
            Test1 t1 = new Test1();
            int[] array1 = {2, 11, 211, 985, 141};
            //1. The number of actual parameter can range from 0 to n.(n = 1, 2, 3, 4, 5......)

            System.out.println(t1.getSum(0, 1, 2, 3, 4, 5));
            System.out.println(t1.getSum(array1));
            //2. The actual parameter can be an array directly.
            System.out.println("------------------------");

    }
}

class Test1 {
    public int getSum(int... parameters) {
        System.out.println(parameters.length + " parameters were received.");
        //3. The variable parameter is actually an array, so you can use it as an array.

        int num = 0;
        for (int i = 0; i < parameters.length; ++i) {
            num += parameters[i];
        }

        System.out.print("The Sum of parameters passed is : ");

        return num;
    }
}

        Output result :

        2.Show notice four : 

package csdn.varparameter;

public class VariableParameter {
    public static void main(String[] args) {
            Test2 t2 = new Test2();

            int[] array1 = {2, 11, 211, 985, 141};
            int[] array2 = {1, 2, 7};

            System.out.println(t2.getSum2(array1, 1, 3, 1));
            System.out.println(t2.getSum2(array1, array2));
            System.out.println("------------------------");
    }
}


/*
    4. When variable parameter is used with ordinary parameter, 
    make sure that the variable parameter is at last of the list of formal parameter.
 */
class Test2 {
    public int getSum2(int[] arr, int... parameters) {
        System.out.println(parameters.length + " parameters were received.");

        int sum1 = 0;
        int sum2 = 0;

        for (int i = 0; i < arr.length; ++i) {
            sum1 += arr[i];
        }   //Use sum1 variable to store the sum of "arr" array.

        for (int i = 0; i < parameters.length; ++i) {
            sum2 += parameters[i];
        }   //Use sum2 variable to store the sum of "variable parameter".

        System.out.print("The total sum of parameters passed is : ");

        return sum1 + sum2;
    }
}

        Output result :

        3.Show notice five : 

         Actually it's easy to understand the notice five, because we have got the notice four :  When variable parameter is used with ordinary parameter, make sure that the variable parameter is at last of the list of formal parameter. So, if you have variable parameters, more than one, you can't promise that every variable parameters is at the end of the the list of formal parameters. Absolutely you can't achieve it!

        Let's have a look at the demonstration,a GIF picture, as follow:

        Apparently, we can see the IDEA report a error, and the reason is actually what we're talking about above.         

        4.Exercise:

        requirement : Encapsulate one static method in a arbitrary class, whatever, and the formal parameters you need to pass in are Student's name, Student's grade and the scores of his or her every subjects.We supposed that the Student totally examines five subject. When this method is invoked, the student's name, grade and five subjects' sum scores will be printed on the console.

        Code Demonstration

package csdn.varparameter;

public class VariableParameter {
    public static void main(String[] args) {
        System.out.println(Test3.studentScore("Cyan", 14, 141,135,80,75));
    }
}

class Test3 {
    public static String studentScore(String name, int grade, int... scores) {
        int scoreNum = 0;
        for (int i = 0; i < scores.length; ++i) {
            scoreNum += scores[i];
        }

        return name + ", who is in " + grade + " class, got totally " + scoreNum + " points";
    }
}

        Output result :

    

System.out.println("END---------------------------------------------------------------------"); 

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cyan_RA9

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值