Java Variable length parameters

Source: Internet
Author: User

Variable length form parameters in Java

Format

public static void add(int ... A)

{

}

Use this example to introduce

Characteristics:

(1) Can receive an argument of any number of the corresponding type , and an argument can be an array

(2) When there are other parameters in the code, the variable-length parameter is placed at the end of the formal parameter list

public static void Add (double num,int ... b

(3) The process of realizing the deformable parameter is also implemented by an array:

A is equivalent to an array name, such as writing a summation method

/* variable-length parameters */import Java.util.Scanner;   public class canchange{public static void Main (String[]args) {//int of arbitrary length []num={34,45,65,7,83,34,34,45,65,}; add (num); }//b is a variable-length parameter public static void Add (int ...  b) {int sum=0; for (int i=0;i<b.length;i++) {sum+=b[i];} System.out.println ("sum=" +sum); } }

This article is from the "11890208" blog, please be sure to keep this source http://11900208.blog.51cto.com/11890208/1865373

Java Variable length parameters

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.