How to convert LinkedList to array in Java? Example

You can convert a LinkedList to an array in Java by using the toArray() method of LinkedList. The toArray() method accepts an array of relevant type to store contents of LinkedList. It stores the elements in the same order they are currently inside LinkedList. By using the toArray() method you can convert any type of LinkedList e.g. Integer, String or Float to any type of Array, only catch is this you cannot convert a LinkedList to an array of primitives i.e. a LinkedList of Integer cannot be converted into an array of ints by using using toArray() method, but you can convert it to array of Integer objects, that's perfectly ok. Similarly, you can convert LinkedList of Double to an array of Double and LinkedList of Float objects to an array of floats.

Java Collection framework is vast as it contains many classes for the different purpose. The best way to master Collection framework is to pick up a good book and follow it from start to end e.g. Java Generics and Collection, which provides a comprehensive coverage of all important classes of Java Collection framework.

Alternatively, you can also follow a good core Java book e.g. Core Java for Impatient by Cay S. Horstmann, which also coverers changes made in Java 8 e.g. lambda expression and streams, which has completely changed how you use Collection classes in Java.

How to convert LinkedList to array in Java with example

Important points about toArray() methods

Since toArray() method is used to convert LinkedList to array in Java, it's important to learn more about it.

1) This method returns an array containing all of the elements in the linked list in same sequence i.e. it keeps the order intact.

2) It accepts the caller to provide an array of specified type, but it's smart enough to make some adjustments to length.

3) If given array is not big enough to store all elements of the LinkedList a new array is created of the same runtime Type and size of the LinkedList.

4) If given array is bigger than the linked list than spare buckets are set to null. You can useful to determine true length of the array if you know that list cannot contain null elements.

Java Program to convert LinkedList to array

Here is a sample Java program which shows you can convert a LinkedList to an array in Java. This program contains two examples, first one convert a LinkedList of String to an array and the second one converts LinkedList of Integer to an array of Integer. As I said, you cannot convert LinkedList of wrapper objects to an array of primitive objects e.g. LinkedList of Double cannot be converted to an array of double primitives.

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

public class LinkedListToArray {

public static void main(String args[]){

// creating and initializing a LinkedList of String
LinkedList<String> listOfBooks = new LinkedList<>();
listOfBooks.add("Effective Java");
listOfBooks.add("Clean Code");
listOfBooks.add("Clean Coder");

// printing the contents of LinkedList before conversion
System.out.println("LinkedList: " + listOfBooks);

// Converting the LinkedList to array
String[] arrayOfBooks = listOfBooks.toArray(new String[listOfBooks.size()]);

// printing contents of array after conversion
System.out.println("String array: " + Arrays.toString(arrayOfBooks));

// Second example - Creating LinkedList of Integers
LinkedList<Integer> listOfScores = new LinkedList<>();

// printiing LinkedList
System.out.println("LinkedList: " + listOfScores);

// converting LinkedList of Integer to array of integers
// int[] score = listOfScores.toArray(new int[listOfScores.size()]); // compile time error
Integer[] scores = listOfScores.toArray(new Integer[listOfScores.size()]); // this is ok

// printing array
System.out.println("Integer array: " + Arrays.toString(scores));

LinkedList: [Effective Java, Clean Code, Clean Coder]
String array: [Effective Java, Clean Code, Clean Coder]
LinkedList: [100, 171, 264]
Integer array: [100, 171, 264]

That's all about how to convert a LinkedList to array in Java. Just remember that you can use toArray() method for this conversion. It also maintains the order of elements and can create a new array if given array is not big enough to store all elements.

Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures and Algorithms: Deep Dive Using Java

Related LinkedList and Java Collection tutorials, articles
  • Core Java for Really Impatient (book)
  • How to add an element at first and the last position of LinkedList in Java? (answer)
  • The difference between LinkedHashMap and HashMap in Java? (answer)
  • The difference between ArrayList and LinkedList in Java? (answer)
  • How to find the length of singly linked list in Java? (solution)
  • How to find middle element of linked list in Java? (answer)
  • How to find if linked list contains a loop in Java? (answer)
  • A comprehensive guide to Java Collection (read here)

No comments:

Post a Comment