Convert linked list to array – Java Program to Convert LinkedList to Array

Convert linked list to array: In the previous article, we have seen Java Program to Print the Array Element Address When the Base Address and Array Element Size is Given

In this article we are going to see how to Convert Linked List to array using java programming language.

Java Program to Convert LinkedList to Array

Array:

Java linkedlist to array: Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

LinkedList:

Linkedlist to array: LinkedList is a linear data structure however the elements are not stored in contiguous memory location as like array rather they are scattered and connected through previous and next link. Where this previous link refers to the address of previous element and next link refers to the address of next element.

In Java, LinkedList Class is a part of Collection framework which provides linked list data structure. It uses the double linked list concept to store the elements.

Let’s see different ways to convert linkedlist to array.

Method-1: Java Program to Convert LinkedList to Array By Static Initialization of LinkedList Elements (Iterative Approach)

Approach:

  1. Create an empty array of the size of the Linked list.
  2. Iterate over the linked list and put each element into the array.

Program:

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

public class Main
{
    public static void main(String[] args) 
    {
       // Initialize the LinkedList
        LinkedList<Integer> list = new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);

        // Print the LinkedList
        System.out.println("Linked list: " + list);
        System.out.println("Array: " + Arrays.toString(convert(list)));
    }

    //converting linkedlist to array
    static int[] convert(LinkedList<Integer> list) 
    {
        int[] arr = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            arr[i] = list.get(i);

        }
        return arr;
    }
}

Output:

Linked list: [1, 2, 3, 4, 5]
Array: [1, 2, 3, 4, 5]

Method-2: Java Program to Convert LinkedList to Array By Dynamic Initialization of LinkedList Elements (Using Inbuilt Array Methods)

Approach:

  • Create scanner class object.
  • Ask user for the length of the Linked list.
  • Initialize the linked list.
  • Convert the linked list into object array using toArray() method.
  • Convert the object array into String array using Arrays.copyOf() method.

Program:

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

public class Main
{
    public static void main(String[] args) 
    {
        // initialize the LinkedList
        LinkedList<String> list = new LinkedList<>();
        // create a Scanner object
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of elements: ");
        int n = sc.nextInt();
        sc.nextLine();// to consume the enter key
        System.out.println("Enter the elements: ");
        for (int i = 0; i < n; i++) 
        {
            list.add(sc.next());
        }

        // Print the LinkedList
        System.out.println("Linked list: " + list);
        System.out.println("Array: " + Arrays.toString(convert(list)));
    }

    //Converting linkedlist to array
    static String[] convert(LinkedList<String> list) 
    {
        // convert the LinkedList to an object array
        Object[] arr = list.toArray();
        // convert the Object array to a String array
        String[] result = Arrays.copyOf(arr, arr.length, String[].class);
        return result;
    }
}

Output:

Enter the number of elements: 5
Enter the elements: 
10 20 30 40 50
Linked list: [10, 20, 30, 40, 50]
Array: [10, 20, 30, 40, 50]

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Related Java Programs: