Use of the Aslist () method of the Arrays tool class
* A: Case Presentation
* Use of the Aslist () method of the Arrays tool class
* Collection ToArray (t[] a) generic version of the set-to-go array
Array-to-collection: Use of the Aslist () method of the Arrays tool class
public static void Demo2 () {
//int[] arr = {11,22,33,44,55};
//list<int[]> List = arrays.aslist (arr); An array of basic data types is converted to a collection, and the entire array is converted as an object, since the collection stores all references to objects. The base data type can only be used as a reference to an object
//system.out.println (list);
Integer[] arr = {11,22,33,44,55};//Convert an array to a collection, the array must be a reference data type so the correct way to convert is integer
list<integer> list = Arrays.aslist (arr);
SYSTEM.OUT.PRINTLN (list);
}public static void Demo1 () {
String[] arr = {"A", "B", "C"};
list<string> list = Arrays.aslist (arr);cannot add delete after converting array to collection
//list.add ("D");//cannot be added
SYSTEM.OUT.PRINTLN (list);
}
================================================================
Aggregate to array: Collection in ToArray (t[] a) generic version of the set-to-go array
The set-to-array, plus the generic
arraylist<string> list = new arraylist<> ();
List.add ("a");
List.add ("B");
List.add ("C");
List.add ("D");
string[] arr = List.toarray (new string[10]); //When the array is converted to an array, the length of the converted array is equal to the size of the collection if it is less than or equal to the size of the collection
If the length of the array is greater than size, the allocated array length is the same as the length you specify
for (String String:arr) {
System.out.println (string);
}
}
Converting a Java base data type array to a collection