Methods for mutual conversion between arrays and lists

Source: Internet
Author: User
1. Convert the list into an array. (The list here indicates that the object is an arraylist)
Call the toarray method of arraylist.
Toarray
Public <t> T [] toarray (T [] A) returns an array containing all elements in the list in the correct order; the runtime type of the returned array is the runtime type of the specified array. If the list can be placed

Returns the array that is placed in the list element. Otherwise, a new array is allocated based on the running type of the specified array and the size of the list.
If the specified array can accommodate the list and has free space (that is, the array has more elements than the list), the elements in the array that closely follow the end of the set are set to null. This is useful for determining the length of the list,

It is only useful when the caller knows that the list does not contain any null element.
Specified:
Toarray in Interface collection <E>
Specified:
Toarray in interface list <E>
Overwrite:
Toarray in abstractcollection <E>
Parameters:
A-array of list elements to be stored, if it is large enough; otherwise, it is a new array allocated for storing list elements with the same runtime type.
Return Value:
An array containing the list elements.
Throw:
Arraystoreexception-if the runtime type of A is not the super type of the runtime type of each element in this list.

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. Convert the array to 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 will be written directly to the array .) This method is the same

Collection. toarray serves as a bridge between array-based APIs and collection-based APIs. The returned list is serializable and implements randomaccess.
This method also provides a convenient way to create a fixed-length list, which is initialized to contain multiple elements:

List stooges = arrays. aslist ("Larry", "Moe", "Curly ");
 
Parameters:
A-list arrays are supported.
Return Value:
List View of the specified array.
For more information, see:
Collection. toarray ()

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.