The collection of Java basics and the conversion of arrays to each other

Source: Internet
Author: User

One, the set conversion to an array:
Using the collection. ToArray () method, depending on the type of the conversion array can be divided into:
1. Convert to object[] Type array
For example: object[] Listarray = List.toarray ();
object[] SetArray = Set.toarray ();
2. Translate to a specific type array
For example: string[] ListArray1 = (string[]) List.toarray (newstring[0])
or string[] ListArray2 = (string[]) List.toarray (Newstring[list.size ()));
String[] SetArray1 = (string[]) Set.toarray (newstring[0]);
or String SetArray2 = (string[]) Set.toarray (Newstring[set.size ()));
Second, the array is converted into a set:
Use the array. Aslist () method
List List = new ArrayList ();
List = arrays.aslist (array); Note: for int[] Arrays You cannot do this directly, because the parameters of the Aslist () method must be an object. Int[] should first be converted to integer[].
Set set = New HashSet (array.aslist (Array)); Use list to construct set

The collection of Java basics and the conversion of arrays to each other

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.