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

﹏ヽ暗。殇╰゛Y 2023-09-25 13:54 182阅读 0赞

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 :

  1. package csdn.varparameter;
  2. public class VariableParameter {
  3. public static void main(String[] args) {
  4. Test1 t1 = new Test1();
  5. int[] array1 = {2, 11, 211, 985, 141};
  6. //1. The number of actual parameter can range from 0 to n.(n = 1, 2, 3, 4, 5......)
  7. System.out.println(t1.getSum(0, 1, 2, 3, 4, 5));
  8. System.out.println(t1.getSum(array1));
  9. //2. The actual parameter can be an array directly.
  10. System.out.println("------------------------");
  11. }
  12. }
  13. class Test1 {
  14. public int getSum(int... parameters) {
  15. System.out.println(parameters.length + " parameters were received.");
  16. //3. The variable parameter is actually an array, so you can use it as an array.
  17. int num = 0;
  18. for (int i = 0; i < parameters.length; ++i) {
  19. num += parameters[i];
  20. }
  21. System.out.print("The Sum of parameters passed is : ");
  22. return num;
  23. }
  24. }

Output result :

f58d5f04eaef46be9efa4968f339a444.png

2.Show notice four :

  1. package csdn.varparameter;
  2. public class VariableParameter {
  3. public static void main(String[] args) {
  4. Test2 t2 = new Test2();
  5. int[] array1 = {2, 11, 211, 985, 141};
  6. int[] array2 = {1, 2, 7};
  7. System.out.println(t2.getSum2(array1, 1, 3, 1));
  8. System.out.println(t2.getSum2(array1, array2));
  9. System.out.println("------------------------");
  10. }
  11. }
  12. /*
  13. 4. When variable parameter is used with ordinary parameter,
  14. make sure that the variable parameter is at last of the list of formal parameter.
  15. */
  16. class Test2 {
  17. public int getSum2(int[] arr, int... parameters) {
  18. System.out.println(parameters.length + " parameters were received.");
  19. int sum1 = 0;
  20. int sum2 = 0;
  21. for (int i = 0; i < arr.length; ++i) {
  22. sum1 += arr[i];
  23. } //Use sum1 variable to store the sum of "arr" array.
  24. for (int i = 0; i < parameters.length; ++i) {
  25. sum2 += parameters[i];
  26. } //Use sum2 variable to store the sum of "variable parameter".
  27. System.out.print("The total sum of parameters passed is : ");
  28. return sum1 + sum2;
  29. }
  30. }

Output result :

18005c6ba6e14188a5978dc9b95db0f7.png

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:

1095b8eaf86b455d9cd20bee78243118.gif

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 :

  1. package csdn.varparameter;
  2. public class VariableParameter {
  3. public static void main(String[] args) {
  4. System.out.println(Test3.studentScore("Cyan", 14, 141,135,80,75));
  5. }
  6. }
  7. class Test3 {
  8. public static String studentScore(String name, int grade, int... scores) {
  9. int scoreNum = 0;
  10. for (int i = 0; i < scores.length; ++i) {
  11. scoreNum += scores[i];
  12. }
  13. return name + ", who is in " + grade + " class, got totally " + scoreNum + " points";
  14. }
  15. }

Output result :

d52c8675ded944df8f9d2a31daf17ea1.jpeg

System.out.println(“END——————————————————————————————————-“);

发表评论

表情:
评论列表 (有 0 条评论,182人围观)

还没有评论,来说两句吧...

相关阅读

    相关 how to use kvm

    kvm 是qemu 集成了一些針對開啟了VT技術的cpu而開發的不錯的內核級程式. 下面是一些簡要的說明:- <table style="border:1px solid