Conversions between Java lists, collections, and arrays

Source: Internet
Author: User

Importjava.util.ArrayList; Importjava.util.Arrays; ImportJava.util.HashSet; Importjava.util.List; ImportJava.util.Set;  Public classTest2 { 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 ()); }    }  

Attention::

If you have an array, you need to convert it into a collection, and the Array.aslist wrapper can do this, for example:

String[] values = ...;

hashset<string> staff = new Hashset<string> (Array.aslist (values));

Conversely, if you want to get an array from a collection, it's a little tricky to manipulate. Of course, you can also use the ToArray method directly:

Object[] values = Staff.toarray ();

However, the result of the method run gets an array of objects. You cannot use type conversions even if you know that the collection contains an object of a specific set of types:

String[] values = (string[]) Staff.toarray ();//error

The array returned by the ToArray method is a object[] array, and you cannot change its type. Instead, you must use some variant of the ToArray method, giving it an array of length 0 that you prefer to use. The returned array is then the same array as the type.

String[] values = Staff.toarray (new string[0]);

If you want, you can also build an array of the specified size:

Staff.toarray (New String[staff.size ()]);

In this scenario, the new array is not created.

Conversions between Java lists, 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.