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