Java Array List conversion method

Source: Internet
Author: User

The 1.List is converted to an array. (Here list it is the entity is ArrayList)

Call the ToArray method of ArrayList.

ToArray

Public <T> t[] ToArray (t[] a) returns an array that includes all the elements in this list in the correct order. The execution-time type of the returned array is the execution-time type of the specified array.

If the list can fit into the specified array, the array that is placed in this list element is returned. Otherwise, a new array is allocated based on the execution-time type of the specified array and the size of the list.

Assuming that the specified array holds the list and has the remaining space (that is, the array has more elements than the list), the element immediately following the collection at the end of the array is set to NULL. This is useful for determining the length of a list, but only if the caller knows that the list does not include whatever null element.

Designated by:

ToArray in the interface collection<e>

Designated by:

ToArray in the interface list<e>

Covered:

ToArray in the class abstractcollection<e>

Number of references:

A-an array of list elements to store. If it is large enough, otherwise it is a new array with the same execution-time type allocated for storing the list elements.

Return:

An array that includes the list elements.

Thrown:

Arraystoreexception-assumes that the execution-time type of a is not a super-type of the execution-time type of each element in this list.

Detailed use Method:

List List = new ArrayList ();
List.add ("1");
List.add ("2");
final int size = List.size ();
String[] arr = (string[]) List.toarray (new string[size]);

2. The array is converted into a list.

Call the Aslist method of arrays.

Aslist

public static <T> list<t> aslist (T ... a) returns a list of fixed sizes supported by the specified array. (Changes to the returned list are "written straight" to the array.) This method is together with Collection.toarray. Serves as a bridge between an array-based API and an collection-based API. The returned list is serializable. and realized the randomaccess.

This method also provides a convenient way to create a fixed-length list. The list is initialized to include multiple elements:

List stooges = arrays.aslist ("Larry", "Moe", "Curly");

Number of references:

A-an array of supported lists.

Return:

Specifies the list view of the array.

See also:

Collection.toarray ()

Detailed use Method:

string[] arr = new string[] {"1", "2"};

List List = Arrays.aslist (arr);

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Java Array List conversion method

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.