1 Array conversion to list
Calls the static method Aslist of the arrays class.
Aslist
public static <T> list<t> aslist (T ... a) Returns a fixed-size List backed by the specified array. (Changes to the returned list, "write through" to the array.) This 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 is also provides a convenient way to create a fixed-size list initialized to contain several:
List<string> stooges = arrays.aslist ("Larry", "Moe", "Curly");
Parameters:
A-the array by which the list would be backed
Returns:
A list view of the specified array
Usage: A method used in the API is provided. More commonly used sample code:
Copy Code code as follows:
string[] arr = new string[] {"str1", "str2"};
list<string> list = Arrays.aslist (arr);
2 list converted to arrays
The list here takes ArrayList as an example, and the ArrayList API provides two functions that can be used.
ToArray
Public object[] ToArray () Returns A array containing all of the elements in this list on proper sequence (from A to LA St Element).
The returned array would be ' safe ' in ' no references to it are maintained by this list. (In the other words, this method must allocate a new array). The caller is thus free to modify the returned array.
This is acts as bridge between Array-based and collection-based APIs.
Specified by:
ToArray in interface collection<e>
Specified by:
ToArray in interface List<e>,
Overrides:
ToArray in class abstractcollection<e>
Returns:
An array containing all of the elements in this list in proper sequence
So:
arrays.aslist (object[])
--------------------------------------------------------------------------------
toArray
Public <T> t[] ToArray (t[] a) Returns A-array containing all of the elements in this list in proper sequence (from F Irst to the last element); The runtime 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 room to spare (i.e., the array has more elements than the list), the element in the array immediately following the "end of" the collection is set to NULL. (This is useful in determining the length of the ' list only if ' caller knows that ' list does not contain no null ele ments.)
Specified by:
ToArray in Interface collection<e>
Specified by:
ToArray in Interface list<e>
Overrides:
ToArray in class abstractcollection<e>
Parameters:
A-the array into which the elements of the list are to being stored, if it is a big enough; Otherwise, a new array of the same runtime type is allocated to this purpose.
Returns:
An array containing the elements of the list
Throws:
Arraystoreexception-if the runtime type of specified array is not a supertype of the runtime type of every element i n this list
Nullpointerexception-if the specified array is null
Usage: Sample code:
Copy Code code as follows:
list<string> list = new arraylist<string> ();
List.add ("str1");
List.add ("str2");
int size = List.size ();
String[] arr = (string[]) List.toarray (new string[size);//Use the second interface, the return value and the parameters are the results