Convert LinkedList to Array Java

Convert LinkedList to Array Java | In this section, we convert the elements of a linked list to an array, the linked list is a linear data structure that stores an element in a linear order, and the elements in the linked list are linked using pointers and addresses and each node contains two parts one contains the element and the other has the address of the next node.

The List interface contains the toArray() method which can be used to convert lists to arrays. It has the following two overloaded forms:-

  1. public T[] toArray(T[] a)
  2. public Object[] toArray()

Convert LinkedList to Array Java Using toArray(T[ ] a)

Let us develop a program to convert linked list to array Java using the public T[ ] toArray(T[ ] a) method,

import java.util.Arrays;
import java.util.LinkedList;

public class Main {
   public static void main(String[] args) {
      LinkedList<String> list = new LinkedList<String>();
      list.add("John");
      list.add("Harry");
      list.add("Madan");
      list.add("Cherry");
      list.add("Robert");
      System.out.println("Linked  list Elements: " + list);
      
      // convert linked list to array
      String[] array = list.toArray(new String[] {});
      System.out.println("Array Elements: " 
                          + Arrays.toString(array));
   }
}

Output:-

Linked list Elements: [John, Harry, Madan, Cherry, Robert]
Array Elements: [John, Harry, Madan, Cherry, Robert]

In the toArray() method we have passed “new String[ ] { }” which is an Anonymous Array. You can also pass “new String[list.size()]”. We have used the Arrays.toString() method to display the array. Also see:- Different ways to Print Array

In the previous example, we have used LinkedList of String. Let us see another program using LinkedList of integer values.

import java.util.Arrays;
import java.util.LinkedList;

public class Main {
   public static void main(String[] args) {
      LinkedList<Integer> list = new LinkedList<>();
      list.add(10);
      list.add(90);
      list.add(30);
      list.add(80);
      list.add(50);
      System.out.println("Linked list Elements: " + list);
      
      // convert linked list to array
      Integer[] array = list.toArray(new Integer[] {});
      System.out.println("Array Elements: " 
                         + Arrays.toString(array));
   }
}

Output:-

Linked list Elements: [10, 90, 30, 80, 50]
Array Elements: [10, 90, 30, 80, 50]

Convert Linked List to Array Java Using toArray()

We have seen program to convert linked list to array Java using toArray(T[ ] a). Now let us see another program using toArray() method. The return type of toArray() method is Object[ ].

import java.util.Arrays;
import java.util.LinkedList;

public class Main {

   public static <T> Object[] convertLinkedListToArray(LinkedList<T> linkedList) {
      return linkedList.toArray();
   }

   public static void main(String args[]) {
      LinkedList<String> List = new LinkedList<String>();
      List.add("J");
      List.add("A");
      List.add("V");
      List.add("A");
      List.add("P");
      List.add("R");
      List.add("O");
      List.add("G");
      List.add("R");
      List.add("A");
      List.add("M");
      System.out.println("Linked list: " + List);

      Object[] objArray = convertLinkedListToArray(List);
      String[] array = Arrays.copyOf(objArray, 
                                     objArray.length,
                                     String[].class);
      objArray = null; 
      System.out.println("Array: " + Arrays.toString(array));
   }
}

Output:-

Linked list: [J, A, V, A, P, R, O, G, R, A, M]
Array: [J, A, V, A, P, R, O, G, R, A, M]

The explanation for the program convert linked list to array Java is as follows:-

Step1:- Get the linked list
Step2:- Convert the LinkedList into an array of objects using the Java method toArray().
Step3:- Then convert the object array to an array by using the Arrays.copyOf() method.
Step4:- Then print the array.

The return type of toArray() method is Object[ ]. Our list is of String type, and after calling toArray() we are getting an array of Objects, hence we have to convert the array of Objects to an array of the string value.

For this purpose, we have used Array.copyOf() method. After that, we assigned null to the objArray (variable, which was storing the array of objects) so that it won’t take memory.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.