Examples of Java mutable parameters: asking for student performance information, not determining the number of courses

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.