Conversion between a Java list and an array

Source: Internet
Author: User

1 Array conversion to list

Call the static method Aslist of the arrays class.

Aslist
aslist (T ... a)
Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method is acts as bridge between Array-based and collection-based APIs, in combination with Collection.toArray() . The returned list is serializable and implements RandomAccess .

This method also provides a convenient-to-create a fixed-size list initialized to contain several elements:

     

Parameters:
a-the array by which the list would be backed
Returns:
A list view of the specified array

usage : A method of use is provided in the API. The more common example code:

1 New String[] {"str1", "str2"}; 2 list<string> List = arrays.aslist (arr);
2 List conversions to arrays

The list here takes ArrayList as an example, and the ArrayList API provides two functions that can be used.

ToArray
ToArray ()
Returns an array containing all of the elements in this list in proper sequence (from first to last
element).

The returned array would be a "safe" in that no references to it is maintained by this list. (In the other words, the This method must allocate a new array). The caller is thus free to modify the returned array.

This method acts as bridge between Array-based and collection-based APIs.

Specified by:
toArrayIn interface Collection<E>
Specified by:
toArrayIn interface List<E>
Overrides:
In
toArray class AbstractCollection<E>
Returns:
An array containing all of the elements in this list in proper sequence
See Also:
Arrays.asList(Object[])

ToArray
ToArray (t[] a)
Returns An array containing any of the elements in this list in proper sequence (from first to last element); the RU Ntime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

If the list fits in the specified array with the guest to spare (i.e., the array had more elements than the list), the Eleme NT in the array immediately following the end of the collection is set to  null . (This was useful in determining the length of the list  only  if the caller knows and the list does not C Ontain any null elements.)

 

Specified by:
toArray  in interface  collection<e>
Specified by:
toArray   In interface  list<e>
Overrides:
ToA Rray  in class  abstractcollection<e>
Parameters:
a-the array into which the elements of the list was to being stored, if it is big enough; Otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
An array containing the elements of the list
Throws:
ArrayStoreException -If the runtime type of the specified array is not a supertype of the runtime type of every element in this LIS T
NullPointerException -If the specified array is null

usage : Sample code:

1 New Arraylist<string>(); 2 list.add ("str1"); 3 list.add ("str2"); 4 int size = list.size (); 5 string[] arr = (string[]) List.toarray (new string[size]); // the second interface is used, and the return value and parameters are both results

Conversion between a Java list and an array

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.