Java Collection Tutorial - Java LinkedList.toArray()








Syntax

LinkedList.toArray() has the following syntax.

public Object [] toArray()

Example

In the following code shows how to use LinkedList.toArray() method.

//from w  ww  . j a  va 2 s  .  c om

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

public class Main {

   public static void main(String[] args) {

      LinkedList<String> list = new LinkedList<String>();

      list.add("Hello");
      list.add("from java2s.com");
      list.add("java tutorial");

      // print the list
      System.out.println("LinkedList:" + list);

      // create an array and copy the list to it
      Object[] array = list.toArray();

      System.out.println(Arrays.toString(array));
   }
}

The code above generates the following result.