LinkedList toArray() Method in Java
Last Updated :
10 Mar, 2025
In Java, the toArray() method is used to convert a LinkedList into an Array. It returns the same LinkedList elements but in the form of an Array only.
Example: Here, we use toArray() to convert a LinkedList into an Array of Integer.
Java
// Java Program to Demonstrate how to use toArray() method
// to convert a LinkedList into an array of Integer elements
import java.util.LinkedList;
class Geeks {
public static void main(String[] args)
{
// Creating an empty LinkedList of Integers
LinkedList<Integer> l = new LinkedList<Integer>();
// Use add() method to add elements
// into the LinkedList
l.add(10);
l.add(20);
l.add(30);
l.add(40);
l.add(50);
System.out.println("The LinkedList: " + l);
// Creating the array
Integer[] arr = new Integer[5];
// using toArray() to convert
// linkedlist into an array
l.toArray(arr);
// Printing all elements of the Array
System.out.print(
"After converting LinkedList to Array: ");
for (Integer element : arr) {
System.out.print(element + " ");
}
}
}
OutputThe LinkedList: [10, 20, 30, 40, 50]
After converting LinkedList to Array: 10 20 30 40 50
Now there are two method to convert LinkedList into an Array i.e. one with toArray() without parameter and one with toArray(arrayName) with parameter.
1. toArray() - Without Parameter
In Java, the toArray() method without parameter returns an array containing all the elements in the list in proper sequence . 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.
Syntax:
Object[] toArray();
- Parameters: It does not take any parameter.
- Return Type: It returns an array of object containing all the elements in the list.
Example: Here, we use toArray() without parameter to convert a LinkedList into an Array of Integer.
Java
// Java Program Demonstrate toArray()
// method of LinkedList
// without parameter (with Integer type LinkedList)
import java.util.LinkedList;
public class Geeks {
public static void main(String[] args)
{
// Create object of LinkedList
LinkedList<Integer> l = new LinkedList<Integer>();
// use add() to insert elements in a LinkedList
l.add(1);
l.add(2);
l.add(3);
l.add(4);
System.out.println("The LinkedList is: " + l);
// Convert LinkedList into an Array
// the method has no parameter
Object[] arr = l.toArray();
// Displaying all elements of the Array
System.out.print(
"After converted LinkedList to Array: ");
for (Object i : arr)
System.out.print(i + " ");
}
}
OutputThe LinkedList is: [1, 2, 3, 4]
After converted LinkedList to Array: 1 2 3 4
2. toArray(arrayName) - With Parameter
In Java, the toArray() method with parameter also returns an array containing all the elements in the list in proper sequence.
- If the array is larger than the LinkedList, the toArray() method fills the array with the elements from the LinkedList, and the remaining positions are filled with null.
- If the array is smaller than the LinkedList, the toArray() method creates a new array of the correct size and fills the array with the elements from the LinkedList.
Syntax:
<T> T[] toArray(T[] a)
- Parameters: It takes an array as a parameter. which should be of the same type as the element of the LinkedList.
- Return Type: It return an array containing elements similar to the LinkedList.
Exceptions: The method might throw two types of exceptions
- ArrayStoreException: When the mentioned array is of a 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.
Example: Here, we use toArray(arrayName) with parameter to convert a LinkedList into an Array of String.
Java
// Java code to Demonstrate the working of toArray(arr[])
// with parameters to convert a LinkedList into a Array of String
import java.util.LinkedList;
public class Geeks{
public static void main(String args[])
{
// Creating an empty LinkedList
LinkedList<String> l = new LinkedList<String>();
// Use add() method to insert
// elements into the LinkedList
l.add("Welcome");
l.add("To");
l.add("Geeks");
l.add("For");
l.add("Geeks");
System.out.println("The LinkedList: " + l);
// Creating the array and using toArray()
String[] arr = new String[5];
l.toArray(arr);
// Displaying all elements of the Array
System.out.print(
"After converted LinkedList to Array: ");
for (String e : l)
System.out.print(e + " ");
}
}
OutputThe LinkedList: [Welcome, To, Geeks, For, Geeks]
After converted LinkedList to Array: Welcome To Geeks For Geeks
Similar Reads
LinkedList lastIndexOf() Method in Java In Java, the lastIndexOf() method of LinkedList is used to find the last occurrence of the specified element in the list. If the element is present it will return the last occurrence of the element, otherwise, it will return -1.Syntax of LinkedList lastIndexOf() Method public int lastIndexOf(Object
2 min read
LinkedList listIterator() Method in Java In Java, the listIterator() method of the LinkedList class returns a ListIterator that allows us to iterate over the elements of the list.Example: Java// Java Program to Demonstrate the // use of listIterator() in LinkedList import java.util.LinkedList; import java.util.ListIterator; public class Ge
3 min read
Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java In Java, the LinkedList class is present in java.util package, and it also has different methods that do the work of flexible addition of elements and help addition both at the front and back of the list. In this article, we cover the LinkedList offer(), offerFirst(), and offerLast() methods. These
5 min read
Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java Linked list class offers the functionality to "look into" the first and last elements of the list and hence can be useful in cases where only the retrieval is required and not necessarily the deletion is required. Three functionalities are present and all are discussed in this article. 1. peek() : T
4 min read
Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java Java's Linked list class offers a function that allows a "Queue Based" working called poll(). This function not only returns deletes the first element, but also displays them while being deleted and hence can have a lot of usage in daily life problems and competitive programming as well. There are 3
4 min read
LinkedList pop() Method in Java In Java, the pop() method of the LinkedList class is used to remove and return the top element from the stack represented by the LinkedList. The method simply pops out an element present at the top of the stack. This method is similar to the removeFirst method in LinkedList.Example 1: Here, we use t
2 min read
LinkedList push() Method in Java In Java, the push() method of the LinkedList class is used to push an element at the starting(top) of the stack represented by LinkedList. This is similar to the addFirst() method of LinkedList. This method simply inserts the element at the first position or top of the LinkedList.Syntax of LinkedLis
1 min read
LinkedList removeFirst() Method in Java In Java, the removeFirst() method of the LinkedList class is used to remove and return the first element of the list.Example 1: Here, we use the removeFirst() method to remove the first element (head) of the LinkedList of Integers.Java// Java Program to demonstrate the // use of removeFirst() in Lin
2 min read
LinkedList removeFirstOccurrence() Method in Java In Java, the removeFirstOccurrence() method of the LinkedList class is used to remove the first occurrence of the specified element from the list. If there is no occurrence of the specified element the list remains unchanged.Example 1: Here, we use the removeFirstOccurence() method to remove the fir
2 min read
LinkedList removeLast() Method in Java In Java, the removeLast() method of LinkedList class is used to remove and return the last element of the list.Example 1: Here, we use the removeLast() method to remove and return the last element (tail) of the LinkedList.Java// java Program to demonstrate the // use of removeLast() in LinkedList im
2 min read