Conversion of arrays and lists in java

Source: Internet
Author: User

1. Convert the List into an array. (Here the List is the object is 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 in the specified array, an array containing the list elements is returned. 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 a list, but only when the caller knows that the list does not contain any null element. Operator: toArray in Interface Collection <E>: toArray in interface List <E> overwrites: toArray parameter in class AbstractCollection <E>: 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: 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. converts an array to a List. Call the asList method. asList public static <T> List <T> asList (T... a) of Arrays to return a fixed-size List supported by the specified array. (Changes to the returned list will be written directly to the array .) Together with Collection. toArray, this method 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. The List is initialized to contain multiple elements: List stooges = Arrays. asList ("Larry", "Moe", "Curly"); parameter: a-list array supported. Return: List View of the specified array. For more information, see Collection. toArray () usage: String [] arr = new String [] {"1", "2"}; List list = Arrays. asList (arr); String [] aa = new String [] {"2", "3", "4"}; String [] s = {"", "B", "c"}; List list1 = Arrays. asList (s); List list2 = java. util. arrays. asList (aa );

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.