Sai A Sai A
Updated date Jul 08, 2023
In this blog, we will learn how to convert a LinkedList to an array in Java. Discover multiple methods, including using a for loop and the built-in toArray() method.

Introduction:

In Java, LinkedLists are dynamic data structures that offer flexibility in adding or removing elements. However, there are scenarios where converting a LinkedList to an array becomes necessary. Arrays provide advantages like random access and improved performance for specific operations. This blog post will explore various methods to convert a LinkedList to an array in Java. By the end, you'll have a complete understanding of these conversion techniques and be able to choose the most suitable method for your specific requirements.

Method 1: Using a for loop

One straightforward approach to converting a LinkedList to an array is by iterating through the LinkedList using a for loop and copying each element to the corresponding index of the array. Let's see this method in action:

import java.util.LinkedList;

public class LinkedListToArrayExample {
    public static void main(String[] args) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        linkedList.add(10);
        linkedList.add(20);
        linkedList.add(30);
        
        int[] array = new int[linkedList.size()];
        
        for (int i = 0; i < linkedList.size(); i++) {
            array[i] = linkedList.get(i);
        }
        
        // Print the array
        for (int value : array) {
            System.out.println(value);
        }
    }
}

Output:

10
20
30

In this method, we first create a LinkedList and add some elements to it. Then, we initialize an array with the same size as the LinkedList. Using a for loop, we iterate over the LinkedList and copy each element to the corresponding index of the array using the get() method. Finally, we print the elements of the array.

Method 2: Using the toArray() method

Java's LinkedList class provides an inbuilt method called toArray(), which simplifies the process of converting a LinkedList to an array. Let's see how to use this method:

import java.util.LinkedList;

public class LinkedListToArrayExample {
    public static void main(String[] args) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        linkedList.add(10);
        linkedList.add(20);
        linkedList.add(30);
        
        Integer[] array = linkedList.toArray(new Integer[linkedList.size()]);
        
        // Print the array
        for (int value : array) {
            System.out.println(value);
        }
    }
}

Output:

10
20
30

In this method, we create a LinkedList, add elements to it, and then call the toArray() method on the LinkedList instance. This method internally converts the LinkedList to an array and returns it. We pass a new Integer array of the LinkedList's size as an argument to ensure the correct array type is created.

Conclusion:

Converting a LinkedList to an array in Java can be accomplished using multiple methods. In this blog post, we explored two common approaches: using a for loop and the toArray() method. The for-loop method provides a straightforward implementation, while the toArray() method simplifies the conversion process.

Comments (0)

There are no comments. Be the first to comment!!!