Variable parameters:
Format Type ... Variable
Essentially, this parameter is an array.
Pros: 1. You can pass an array when you pass a parameter, or you can pass multiple values.
2. You can not pass a value.
Arrays.aslist () can convert an array to a list collection.
Note: A fixed-length list collection is obtained.
Cause: Because the array length is fixed, the collection is converted from an array.
Why would you want to convert an array into a collection?
Provides richer operations in the collection.
Int[] arr = {1, 2, 3};
List List = Arrays.aslist (arr);
System.out.println (List.size ());
SYSTEM.OUT.PRINTLN (list);
Converts a int[] into a list collection using the Arrays.aslist method
The resulting set length is 1, the element in the collection is int[] why?
The elements in the collection are object.
Because the elements in the collection we are converting now are of type int and cannot be loaded directly into the collection (nor boxed)
The array object is then made to the collection object. List<int[]>
-------------------------------
PackageCn.itcast.param;Importjava.util.Arrays;Importjava.util.List;Importorg.junit.Test;//variable Parameters Public classDemo {// //the number of two int is calculated and//public int sum (int a, int b) {//return a + B; // } // // //ask for three numbers and//public int sum (int a, int b, int c) {//return a + B + C; // } // //the number of N and---Use array to solve//public static int sum (int[] arr) {//int sum = 0; //for (int i = 0; i < arr.length; i++) {//sum + = Arr[i]; // } //return sum; // } Public Static intSumint... a) { intsum = 0; for(inti = 0; i < a.length; i++) {sum+=A[i]; } returnsum; } Public Static voidMain (string[] args) {//ask for two numbers and intA = 10; intb = 20; //System.out.println (SUM (new int[] {A, b}));//manual packaging is required. Wraps the data into an array. //for our developers, the use of uncomfortable. ------------want to package, packaging, do not want to wrap it.----can use variable parameters.System.out.println (sum (A, b)); SUM (); } //there can be only one variable parameter in a method, and this variable parameter must be written at the end, and cannot be followed by another parameter//Public void Demo (String...s,int b) {} variable parameter cannot be used after a parameter//Public void Demo (STRING...S1,STRING...S2) {} There can be only one mutable parameter within a method. //Public void Demo (int b,string...s) {}//you can only add other parameters before the variable parameter. //Variable parameter Example arrays.aslist () its role can transform an array into a collection.@Test Public voidDemo () {//string[] s = {"A", "B", "C"}; //List List = Arrays.aslist (s);//get a fixed-size list. // // //List.add ("D"); //System.out.println (list); int[] arr = {1, 2, 3 }; List List=arrays.aslist (arr); System.out.println (List.size ()); SYSTEM.OUT.PRINTLN (list); }}
JDK5.0 new Features-variable parameters