Java LinkedList toArray() Method

There are two LinkedList.toArray() overloaded methods in Java are used to convert a LinkedList into an array. This guide will cover the usage of these methods, explain how they work, and provide examples to demonstrate their functionality.

Table of Contents

  1. Introduction
  2. toArray Method Syntax
  3. Examples
    • Converting LinkedList to Object Array
    • Converting LinkedList to Typed Array
  4. Conclusion

Introduction

The LinkedList.toArray() overloaded methods are members of the LinkedList class in Java. They allow you to convert a LinkedList to an array, either as an array of Object or as a typed array. These methods are particularly useful when you need to work with array data structures or pass the list elements to methods that require arrays.

toArray Method Syntax

Converting LinkedList to Object Array

public Object[] toArray()
  • Returns: An array containing all of the elements in this list in proper sequence (from first to last element).

Converting LinkedList to Typed Array

public <T> T[] toArray(T[] a)
  • a: The array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
  • Returns: An array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

Examples

Converting LinkedList to Object Array

The toArray() method can be used to convert a LinkedList to an array of Object.

Example

import java.util.LinkedList;

public class ToArrayExample {
    public static void main(String[] args) {
        // Creating a LinkedList of Strings
        LinkedList<String> list = new LinkedList<>();

        // Adding elements to the LinkedList
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        // Converting LinkedList to Object array
        Object[] array = list.toArray();

        // Printing the elements of the array
        System.out.println("Array elements:");
        for (Object element : array) {
            System.out.println(element);
        }
    }
}

Output:

Array elements:
Apple
Banana
Orange

Converting LinkedList to Typed Array

The toArray(T[] a) method can be used to convert a LinkedList to a typed array.

Example

import java.util.LinkedList;

public class ToTypedArrayExample {
    public static void main(String[] args) {
        // Creating a LinkedList of Strings
        LinkedList<String> list = new LinkedList<>();

        // Adding elements to the LinkedList
        list.add("Apple");
        list.add("Banana");
        list.add("Orange");

        // Converting LinkedList to a typed array
        String[] array = list.toArray(new String[0]);

        // Printing the elements of the array
        System.out.println("Array elements:");
        for (String element : array) {
            System.out.println(element);
        }
    }
}

Output:

Array elements:
Apple
Banana
Orange

Conclusion

The LinkedList.toArray() methods in Java provide ways to convert a LinkedList to an array, either as an array of Object or as a typed array. By understanding how to use these methods, you can efficiently work with array data structures in your Java applications. Whether you need a generic Object array or a specific typed array, these methods offer flexible solutions for converting list elements to arrays.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top