Conversions between collections and arrays

Source: Internet
Author: User

Arrays and lists, setscan hold multiple elements, the characteristics of the array is fixed length, access is very fast, the element typeis single, the List is characterized by the dynamic increase in length, to maintain the order of elements, all the elements deposited as Object, Allows elements to be duplicated;the set is characterized by a dynamic increase in length, which ensures that elements are not duplicated and that all the elements deposited are treated as Object. This section describes how sets, lists, and arrays are converted, converts elements in a set, List object to arrays, converts an array to a set,list object, and the elements remain constant during the conversion.

Key Technical Analysis:

The key technical points for converting between List, set and array are as follows:

List conversion Arrays You can use the ToArray method of list to return an object array.

Set conversion Arrays can use the ToArray method of set to return an object array.

If the type of the element in list or set is A, you can use the ToArray method with parameters to get an array of type A, with the specific statement "(a[])Set.toarray (new a[0])".

Arrays can be converted to lists using arrays's aslist static method to get a list.

When an array is converted to set, it is necessary to convert the array into a list and then construct the set with the list .

Importjava.util.ArrayList;Importjava.util.Arrays;ImportJava.util.HashSet;Importjava.util.List;ImportJava.util.Set; Public classTestcollectiontoarray { Public Static voidMain (string[] args) {List List=NewArrayList (); List.add (A); List.add ("B"); List.add (C); List.add ("D"); //List.add (1);//can produce java.lang.ArrayStoreException anomalies .//You can convert a list to an array when the data types in the list are consistentobject[] Array =List.toarray (); System.out.println ("The length of the object array converted from list is:" +array.length); //You need to force the type conversion when converting to an array of other types, and to use the ToArray method with parameters, the argument is an array of objects,//The contents of the list are placed in the parameter array, and the length of the array is automatically expanded to fit the list length when the parameter array is less than the number of elements in the list .String[] Array1 = (string[]) List.toarray (NewString[0]); System.out.println ("The length of the string array converted from List is:" +array1.length); //assigns a string array of equal length to the listString[] Array2 = (string[]) List.toarray (Newstring[list.size ()]); System.out.println ("The length of the string array converted from List is:" +array2.length);       List.clear (); //convert an array into a list       for(inti = 0; i < Array.Length; i++) {List.add (array[i]); } System.out.println ("The number of elements to convert an array to a list is:" +list.size ());      List.clear (); //Aslist method for direct use of arraysList =arrays.aslist (array); System.out.println ("The number of elements to convert an array to a list is:" +list.size ()); Set Set=NewHashSet (); Set.add (A); Set.add ("B"); //convert set to arrayArray =Set.toarray (); Array1= (string[]) Set.toarray (NewString[0]); Array2= (string[]) Set.toarray (Newstring[set.size ()]); System.out.println ("The length of the object array converted from set is:" +array.length); System.out.println ("The length of the string array converted from set is:" +array2.length); //The array is converted into set//after converting an array to a list, the list is used to construct the setSet =NewHashSet (arrays.aslist (array)); System.out.println ("The number of elements to convert an array to set is:" +list.size ()); //clears the set and then converts the array to the list all addset.clear ();      Set.addall (Arrays.aslist (array1)); System.out.println ("The number of elements to convert an array to set is:" +list.size ()); }}

The output is:

the length of the object array converted from list is:4

the length of the string array converted from list is:4

the length of the string array converted from list is:4

The number of elements that convert an array to a list is:4

The number of elements that convert an array to a list is:4

the length of the object array converted from set is:2

the length of the string array converted from set is:2

The number of elements that convert an array to set is:4

The number of elements that convert an array to set is:4

SOURCE Analysis:

L. When the element type in list or set is single, the ToArray method with parameters can be used, and the parameter is the target array object, if the target array length is smaller than the number of elements of list or set, the target array length is automatically adjusted to L, if the target array is longer than L, the elements of the list or set are placed in the first L position of the target array when the conversion occurs . A forced type conversion is required after conversion to get the target array. array1 = (string[]) Set.toarray (new string[0]), Array2 = (string[]) Set.toarray (new String[set.size ()));

2. When an array is converted to a list or set, it is necessary to use the aslist method of arrays, which transforms the array into a list, which can be used to construct the set. Set set = New HashSet (arrays.aslist (array));

Conversions between collections and arrays

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.