Java Collection Tutorial - Java LinkedList.toArray(T[] a)








Syntax

LinkedList.toArray(T[] a) has the following syntax.

public <T> T[] toArray(T[] a)

Example

In the following code shows how to use LinkedList.toArray(T[] a) method.

/*w w  w.  j  a v  a2 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>();

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

      System.out.println(list);

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

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

   }
}

The code above generates the following result.