Java LinkedList toArray() Method with Examples

In the previous article, we have discussed about Java LinkedList subList() Method with Examples

In this article we are going to see the use of Java LinkedList toArray() method along with suitable examples.

Java LinkedList toArray() Method with Examples

This java.util.LinkedList.toArray() method is used to convert an LinkedList into an Array.

It returns the same LinkedList elements but in the form of Array.

Let’s see toArray() method with suitable examples.

Method-1: toArray() – with parameter

Syntax:

LinkedListName.toArray(ArrayName)

Where,

  • LinkedListName refers to the name of your LinkedList.
  • ArrayName refers to the name of array to which the respective linkedlist will be converted.

Let’s see an example with String Type LinkedList to understand it more clearly.

Example: Java LinkedList toArray() Method – Example with String Type LinkedList

Approach:

  • Create a new LinkedList of type String.
  • Add string elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then create an array of same String type and same size of the LinkedList.
  • Then convert the LinkedList to an array using toArray() method.
  • Print the elements inside the array using for each loop.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l.add("Apple");
        l.add("boy");
        l.add("Dog");
        l.add("egg");
        l.add("Flower");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // Create a new Array of String type
        // size of Array is same as the LinkedList
        String[] l1 = new String[l.size()];
        // Convert LinkedList into an Array
        l.toArray(l1);
        // print all elements of the Array
        System.out.print("After converted to Array: ");
        for(String elements:l1)
        System.out.print(elements+"  ");
    }
}

Output:

The elements of LinkedList are: [Apple, boy, Dog, egg, Flower]
After converted to Array: Apple boy Dog egg Flower

Method-2: toArray() – without parameter

Syntax:

LinkedListName.toArray()

Where,

  • LinkedListName refers to the name of your LinkedList.

Let’s see an example with Integer Type LinkedList to understand it more clearly

Example: Java LinkedList toArray() Method – Example with Integer Type LinkedList

Approach:

  • Create a new LinkedList of type Integer.
  • Add integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then create an Array without a parameter.
  • Then convert the LinkedList to an Array using toArray() method.
  • Print the elements inside the Array using for each loop.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {        
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l.add(2);
        l.add(52);
        l.add(13);
        l.add(17);
        l.add(1);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // Convert LinkedList into an Array
        // the method has no parameter
        Object[] obj = l.toArray ();
        // print all elements of the Array
        System.out.print("After converted to Array: ");
        for(Object element : obj)
            System.out.print(element+"  ");
    }
}

Output:

The elements of LinkedList are: [2, 52, 13, 17, 1]
After converted to Array: 2 52 13 17 1

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Related Java Programs: