1. Use of the Aslist () method in the Arrays tool class
1 Public static <T> list<t> aslist (T ... a): Turn arrays into sets
Precautions:
Although you can turn an array into a collection, the length of the collection cannot be changed .
2. code example:
(1)
1 Packagecn.itcast_03;2 3 Importjava.util.Arrays;4 Importjava.util.List;5 6 /*7 * public static <T> list<t> aslist (T ... a): Convert Array to collection8 * 9 * Precautions:Ten * Although you can turn an array into a collection, the length of the collection cannot be changed. One */ A Public classArraysdemo { - Public Static voidMain (string[] args) { - //define an array the //string[] Strarray = {"Hello", "World", "Java"}; - //list<string> List = arrays.aslist (strarray); - -list<string> list = arrays.aslist ("Hello", "World", "Java"); + for(String s:list) { - System.out.println (s); + } A } at}
The results are as follows:
(2)
1 Packagecn.itcast_03;2 3 Importjava.util.Arrays;4 Importjava.util.List;5 6 /*7 * public static <T> list<t> aslist (T ... a): Convert Array to collection8 * 9 * Precautions:Ten * Although you can turn an array into a collection, the length of the collection cannot be changed. One */ A Public classArraysdemo { - Public Static voidMain (string[] args) { - //define an array the //string[] Strarray = {"Hello", "World", "Java"}; - //list<string> List = arrays.aslist (strarray); - -list<string> list = arrays.aslist ("Hello", "World", "Java"); + //unsupportedoperationexception - //list.add ("Java ee"); + //unsupportedoperationexception A //List.remove (1); atList.set (1, "Java ee")); - - for(String s:list) { - System.out.println (s); - } - } in}
The results are as follows:
Java Basic Knowledge Hardening Collection Framework note 33:arrays Tool class using the Aslist () method