Use of the asList () method of the Arrays tool class in the set framework, arraysaslist

Source: Internet
Author: User

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}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.