Methods of converting arrays and lists into each other

Source: Internet
Author: User
Tags arrays int size
1.List conversion into an array. (The list here is the entity is ArrayList)
Invokes the ArrayList ToArray method.
ToArray
Public <T> t[] ToArray (t[] a) returns an array that contains all of the elements in the list in the correct order; The Run-time type of the array is the Run-time type of the specified array. If the list can be placed in the specified array, the array that is placed in this list element is returned. Otherwise, a new array is allocated based on the Run-time type of the specified array and the size of the list.
If the specified array can hold the list and have the remaining space (that is, the elements of the array are more than the list), the element that is immediately at the end of the collection is set to NULL in the array. This is useful for determining the length of a list, but only when callers know that the list does not contain any null elements.
Designated by:
ToArray in the interface collection<e>
Designated by:
ToArray in the interface list<e>
Covered:
ToArray in the class abstractcollection<e>
Parameters:
A-to store an array of list elements, if it is large enough; otherwise, it is a new array of the same run-time type allocated for storing list elements.
Return:
An array that contains the elements of the list.
Thrown:
Arraystoreexception-If A's run-time type is not a super type of the run-time type for each element in this list.

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

2. Array conversion becomes list.
Invokes the arrays Aslist method.
Aslist
public static <T> list<t> aslist (T. A) returns a fixed size list supported by the specified array. (Changes to the return list are "written straight" to the array.) This method, together with Collection.toarray, serves as a bridge between an array based API and a Collection API. The returned list is serializable and implements the randomaccess.
This method also provides a convenient way to create a fixed-length list that is initialized to include multiple elements:

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

Parameters:
A-an array of supported lists.
Return:
Specifies the list view of the array.
See also:
Collection.toarray ()

Specific usage:
string[] arr = new string[] {"1", "2"};
List List = Arrays.aslist (arr);

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.