The Java.util.LinkedList.toArray() method returns an array containing all the elements in the list in proper sequence i.e. from first to last. The returned array will be safe as a new array is created (hence new memory is allocated). Thus the caller is free to modify the array. It acts as a bridge between array-based and collection-based APIs.
public Object toArray()
Parameters:It does not take in any parameter.
Return Value:It returns an array containing all the elements in the list.
Below examples illustrates the LinkedList.toArray() method:
LinkedList: [7855642, 35658786, 5278367, 74381793] Returned Array: [7855642, 35658786, 5278367, 74381793]
The toArray(arr) method method of LinkedList class in Java is used to form an array of the same elements as that of the LinkedList. It returns an array containing all of the elements in this LinkedList in the correct order; the run-time type of the returned array is that of the specified array. If the LinkedList fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the run time type of the specified array and the size of this LinkedList.
If the LinkedList fits in the specified array with room to spare (i.e., the array has more elements than the LinkedList), the element in the array immediately following the end of the LinkedList is set to null. (This is useful in determining the length of the LinkedList only if the caller knows that the LinkedList does not contain any null elements.)
public <T> T toArray(T a)
Parameters: The method accepts one parameter arr which is the array into which the elements of the LinkedList are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Return Value: The method returns an array containing the elements similar to the LinkedList.
Exception: The method might throw two types of exception:
- ArrayStoreException: When the mentioned array is of the different type and is not able to compare with the elements mentioned in the LinkedList.
- NullPointerException: If the array is Null, then this exception is thrown.
Below program illustrates the working of the LinkedList.toArray(arr) method.
Program 1: When array is of the size of LinkedList
The LinkedList: [Welcome, To, Geeks, For, Geeks] Returned Array: [Welcome, To, Geeks, For, Geeks]
- Set toArray() method in Java with Example
- LinkedList get() Method in Java
- LinkedList set() Method in Java
- LinkedList add() Method in Java
- LinkedList contains() Method in Java
- LinkedList pop() Method in Java
- LinkedBlockingDeque toArray() method in Java with Example
- PriorityBlockingQueue toArray() method in Java
- ArrayDeque toArray() Method in Java
- CopyOnWriteArraySet toArray() method in Java with Example
- ConcurrentLinkedDeque toArray() method in Java with Example
- ConcurrentLinkedQueue toArray() Method in Java
- Stack toArray(T) method in Java with Example
- Stack toArray() method in Java with Example
- TreeSet toArray(T) method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.