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 );