Obtain an array from an ArrayList.
import public Object[]
|
The toArray() method retrieves an array containing all the items in the list.
If no argument is provided, the returned array will have the Object type. If an array is specified as an argument, the method returns an array with the same data type.
If the argument array is large enough to accommodate all list items, the method directly returns the argument after populating it with the list items.
Choose one of the following:
public |
public |
“T” denotes the data type of the elements in the list.
Parameter |
Description |
array |
This parameter is optional. If provided, it determines the data type of the returned array. If the provided array has enough space to accommodate all list items, it will be returned. Otherwise, a new array of the same data type will be created and returned. |
Returns: |
An array that includes all elements of the ArrayList, arranged in their original order. |
Throws: |
ArrayStoreException occurs if the type of the array is incompatible with the type of the ArrayList. NullPointerException is thrown if the argument is null. |
Define the return type of toArray().
import public
|