Mutable parameters: When defining a method, you do not know how many parameters to define
Format:
Modifier returns a value type method name (data type ... Variable Name) {
}
Attention:
The variable here is actually an array
If a method has a variable parameter, and there are multiple parameters, then the variable parameter is definitely the last
1 Importjava.util.Scanner;2 public classArgsdemo {3 4 public Static voidmain (string[] Args) {5 intresult = Sum (1,2,3,4,5,6,7,8,9);//The number of numbers participating in the calculation can be changed arbitrarily6 System.out.println (result);7 }8 9 //An addition method that defines a parameter as a variable parameterTen public Static intSumint.. a) {//the variable here is actually an array one ints = 0; a for(intI:a) { -s + =i; - } the returns; - } - -}
public static <T> list<t> aslist (T ... a): convert arrays to Collections
Precautions:
Although you can turn an array into a collection, the length of the collection cannot be Changed.
1 packagecn.itcast_03;2 3 Importjava.util.Arrays;4 Importjava.util.List;5 6 7 public classArraysdemo {8 public Static voidmain (string[] Args) {9 //define an arrayTen //string[] strarray = {"hello", "world", "java"}; one //list<string> List = arrays.aslist (strarray); a -list<string> list = arrays.aslist ("hello", "world", "java"); - //unsupportedoperationexception the //list.add ("java ee");//cannot increase the number of elements - //unsupportedoperationexception - //List.remove (1);//cannot reduce the number of elements - +List.set (1, "java ee");//without changing the length of the collection, you can - + for(String S:list) { a System.out.println (s); at } - } - } - -
Java 16-13 variable parameters and Aslist () method for arrays tool classes