Variable parameter features:
1) ... Can only appear at the end of the argument list
2) ... Between the variable type and the variable name
3) When invoking a method of a mutable parameter, the compiler creates an array for the mutable parameter implicitly, accessing the mutable parameter as an array in the method body
Mutable parameters can also be used to solve this problem by using an array
Package Test;public class Student {private int []score;//each section results private int scorecount=0;//Total Course number//constructor Studen T (int id,string name,int ... IS) {//Here the variable &--function does not use ID and name, here is just a student scorecount=is.length;//get the size of the variable parameter passed in, can be considered as an "array" Score=new int[ scorecount];//gives course number for (int i=0;i<scorecount;i++) {score[i]=is[i]; }}//Course average score int avg () {int avg=0; for (int i=0;i<scorecount;i++) {avg+=score[i]; } Avg/=scorecount; return avg; }//course sum int sum () {int sum=0; for (int i=0;i<scorecount;i++) {sum+=score[i]; } return sum; The//main function tests public static void main (string[] args) {Student stu1=new Student (101, "Zhangsan", 60,61,59,60,61, 59); Student stu2=new Student (102, "Lisi", 60,61,59); System.out.println ("Student 1 Course Average:" +stu1.avg ()); System.out.println ("Student 2 Course Average:" +stu2.avg ()); }}
Examples of variable parameters in Java: Asking for student performance information, not determining the number of courses