This example shows how to convert LinkedList to an array in Java. This example also shows how to convert a LinkedList of Wrapper objects to an equivalent primitive array using the toArray method.
How to convert LinkedList to an array in Java?
There are several ways using which you can convert a linked list object to an array in Java.
1. By iterating the LinkedList
The simplest way to convert to an array is to iterate the LinkedList and add linked list elements to an array one by one as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.util.Arrays; import java.util.LinkedList; public class LinkedListToArrayExample { public static void main(String[] args) { LinkedList<Integer> linkedListNumbers = new LinkedList<Integer>(); linkedListNumbers.add(1); linkedListNumbers.add(2); linkedListNumbers.add(3); /* * Declare an array with the same type and size * as that of the LinkedList object */ Integer[] intArray = new Integer[ linkedListNumbers.size() ]; int index = 0; /* * Iterate the linked list object and * add elements to an array one by one */ for(Integer number : linkedListNumbers){ intArray[index++] = number; } //print the array System.out.println("Array contains: " + Arrays.toString(intArray)); } } |
Output
1 |
Array contains: [1, 2, 3] |
2. Using the toArray method
You can use the toArray
method of the LinkedList class to convert to an array from the LinkedList object.
1 |
public <T> T[] toArray(T[] a) |
The toArray
method returns an array containing all the elements of the linked list in the proper sequence.
Important Note:
If the specified array is large enough to hold all the elements of the LinkedList, the same array is returned with the list elements. If not, a new array is created, filled with the elements and returned. If the specified array is larger than the linked list, the array element that immediately comes after the list element is set to null to mark the boundary of the list elements.
As a good practice, always pass an array with the same size as that of the LinkedList object to avoid the creation of a new array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
LinkedList<Integer> linkedListNumbers = new LinkedList<Integer>(); linkedListNumbers.add(1); linkedListNumbers.add(2); linkedListNumbers.add(3); /* * To convert LinkedList to an Array, use the * toArray method */ Integer[] intArray = linkedListNumbers.toArray( new Integer[ linkedListNumbers.size() ] ); //print the array System.out.println("Array contains: " + Arrays.toString(intArray)); |
Output
1 |
Array contains: [1, 2, 3] |
3. Convert LinkedList of Wrapper objects to an array of primitive values using Apache Commons
If you are using the Apache Commons library, then the ArrayUtils class provides the toPrimitive
method which can be used to convert LinkedList of wrapper objects to an equivalent array of primitive values along with the toArray
method (for example, LinkedList of Double objects to an array of double).
1 |
static int[] toPrimitive(Integer[] array) |
The toPrimitive
method is overloaded for all primitive types.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
LinkedList<Integer> linkedListNumbers = new LinkedList<Integer>(); linkedListNumbers.add(1); linkedListNumbers.add(2); linkedListNumbers.add(3); /* * To convert LinkedList of Integer to an int array, use * the toPrimitive method along with the toArray method */ int[] intArray = ArrayUtils.toPrimitive( linkedListNumbers.toArray( new Integer[ linkedListNumbers.size() ] ) ); System.out.println("int array contains: " + Arrays.toString(intArray)); |
Output
1 |
int array contains: [1, 2, 3] |
This example is a part of the LinkedList in Java tutorial.
Please let me know your views in the comments section below.
References:
Java 8 LinkedList