Open In App

LinkedList toArray() method in Java with Example

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

java.util.LinkedList.toArray() method is used to convert LinkedList into an Array. It returns the same LinkedList elements but in the form of Array only.

We have two method to convert LinkedList into an Array

  • toArray() – without parameter
  • toArray(arrayName) – with parameter

Method-1: toArray() – without parameter

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. 

Syntax:

LinkedListName.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: 

Example:  

Java




// Java Program Demonstrate toArray()
// method of LinkedList
// without parameter (with Integer type LinkedList)
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // Create object of LinkedList
        LinkedList<Integer> list
            = new LinkedList<Integer>();
  
        // Add numbers to end of LinkedList
        list.add(7855642);
        list.add(35658786);
        list.add(5278367);
        list.add(74381793);
  
        // Prints the LinkedList elements
        System.out.println("LinkedList: " + list);
  
        // Convert LinkedList into an Array the method has
        // no parameter
        Object[] a = list.toArray();
  
        // Print all elements of the Array
        System.out.print(
            "After converted LinkedList to Array: ");
        for (Object element : a)
            System.out.print(element + " ");
    }
}


Output

LinkedList: [7855642, 35658786, 5278367, 74381793]
After converted LinkedList to Array: 7855642 35658786 5278367 74381793 

Method-2: toArray(arrayName) – with parameter

The toArray(arrayName) 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.)

Syntax:

LinkedListName.toArray(ArrayName)

Parameters: The method accepts one parameter arrayName 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: An array containing elements similar to the LinkedList. 

Exception: 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.

Below program illustrates the working of the LinkedList.toArray(arrayName) method. 

Example:  toArray(arrayName) – with parameter (with String type LinkedList)

Java




// Java code to illustrate toArray(arr[])
  
import java.util.*;
  
public class LinkedListDemo {
  
    // Main driver method
    public static void main(String args[])
    {
        // Creating an empty LinkedList
        LinkedList<String> list = new LinkedList<String>();
  
        // Use add() method to add
        // elements into the LinkedList
        list.add("Welcome");
        list.add("To");
        list.add("Geeks");
        list.add("For");
        list.add("Geeks");
  
        // Displaying the LinkedList
        System.out.println("The LinkedList: " + list);
  
        // Creating the array and using toArray()
        String[] arr = new String[5];
        list.toArray(arr);
  
        // Print all elements of the Array
        System.out.print(
            "After converted LinkedList to Array: ");
        for (String elements : list)
            System.out.print(elements + " ");
    }
}


Output

The LinkedList: [Welcome, To, Geeks, For, Geeks]
After converted LinkedList to Array: Welcome To Geeks For Geeks 

The toArray() method in Java is used to convert a linked list into an array. This method returns an array containing all the elements of the linked list in the same order as they appear in the list. Here is an example of how to use the toArray() method in Java:

Java




import java.util.LinkedList;
  
public class LinkedListExample {
  
    public static void main(String[] args)
    {
        // Create a LinkedList of Strings
        LinkedList<String> list = new LinkedList<String>();
        list.add("apple");
        list.add("banana");
        list.add("cherry");
        list.add("date");
        list.add("elderberry");
  
        // Convert LinkedList to Array
        String[] array
            = list.toArray(new String[list.size()]);
  
        // Print the elements of the Array
        for (String fruit : array) {
            System.out.println(fruit);
        }
    }
}


Output

apple
banana
cherry
date
elderberry


Last Updated : 13 Mar, 2024
Like Article
Save Article
Share your thoughts in the comments
Similar Reads