Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Conversion between array and list
Conversion between array and list
1.List is converted into an array. (the List here is an entity or an ArrayList)

Call the toArray method of ArrayList.

toArray

public < T> T[] toArray(T[] a) returns an array containing all the elements in this list in the correct order; The runtime type of the returned array is the runtime type of the specified array. If the list can fit into the specified array, the array into which the list elements are put is returned. Otherwise, a new array will be allocated according to the runtime type of the specified array and the size of this list.

if the specified array can hold the list and there is room left (that is, there are more elements in the array than in the list), the element immediately following the end of the collection in the array will be set to null. This is useful for determining the length of the list, but only if the caller knows that the list does not contain any null elements.

designee:

interface collection <; E> ToArray

specifier in:

interface list <; E> ToArray

overlay in:

class abstract collection <: E> The toArray

parameter in:

a-the array to store list elements, if it is large enough; Otherwise, it is a new array with the same runtime type allocated for storing list elements.

return:

an array containing list elements.

throw:

ArrayStoreException-If the runtime type of A is not the supertype of the runtime type of 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]);

you can also use this method

string [] userid = {"aa", "bb", "cc"};

List< String> userList = new ArrayList< String> ();

Collections.addAll(userList, userid);

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 fixed-size list supported by the specified array. (Changes to the returned list are "written straight" to the array. Together with Collection.toArray, this method acts as a bridge between the array-based API and the collection-based API. The returned list is serializable and RandomAccess is implemented.

this method also provides a convenient way to create a fixed-length list, which is initialized to contain multiple elements:

liststooges = arrays. aslist ("Larry", "moe", "curly");

parameters:

a-an array that supports lists.

return:

list view of the specified array.

See also:

Collection.toArray()

Specific usage:

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

List list = Arrays.asList(arr);