Use of the asList () method of the Arrays tool class in the set framework, arraysaslist
I. Conversion from string array to set
1 String [] str = {"ds", "df", "da", "dfg"}; 2 List <String> array = new ArrayList <> (); 3 array = Arrays. asList (str); 4 System. out. println (array); 5 // array. add ("retu"); 6 // error; elements cannot be added or removed when the array is converted to a set, however, you can use the idea of a set to retrieve the operation set (that is, you can use other methods in the set to operate the collection element)
Ii. convert a non-string array to a set
1 // Case 1: running result: [[I @ 15db9742] 2 int [] arr = {12, 98, 54,87, 946, 67}; 3 List <int []> list = Arrays. asList (arr); // converts an array of basic types to a set, and converts the entire set into an object. out. println (list); 5 // Case 2: running result: [12, 98, 54, 87,946, 67] 6 Integer [] in = {, 67 }; 7 List <Integer> list1 = Arrays. asList (in); // converts an array to a set. The array element type must be of the referenced data type 8 System. out. println (list1 );
3. convert a set to an array
1 // <T> T [] toArray (T [] a) 2 // returns an array containing all elements in the collection; the runtime type of the returned array is the same as the runtime type of the specified array. 3 ArrayList <String> list = new ArrayList <> (); 4 list. add ("AE"); 5 list. add ("op"); 6 list. add ("q"); 7 list. add ("v"); 8 9 String [] str = list. toArray (new String [10]); 10 for (String string: str) {// when the set is converted to an array, if the array length is smaller than or equal to the size of the set, the length of the converted array is equal to the size11 of the set. // if the length of the array is greater than the size of the set, the length of the array is the same as the length you specified. out. println (string); 13}