Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

LinkedList.toArray Method (Object[ ])

Visual Studio 2005

Copies the elements of a LinkedList object into specified array object.

Package: java.util

Assembly: vjslib (in vjslib.dll)

public java.lang.Object[] toArray(
    java.lang.Object[] arr);



The array object that receives the elements of the LinkedList object.

The array object that contains the LinkedList elements.

In this example, you create an initialize a LinkedList collection, convert it to an array object "s," and then you copy this object to a new array object "s1."

// LList-toArr2.jsl
// LinkedList.toArray example

import java.util.*;

public class MyClass
    public static void main(String[] args)
        // Create a linked list object:
        LinkedList lList =  new LinkedList();

        // Add some elements:

        // Create an array "s" from the list:
        Object[] s = lList.toArray();

        // Copy "s" to another object array "s1":
        Object[] s1 = lList.toArray(s);

        // Display the array objects:
        System.out.println("The array objects are:");
        for (int i=0; i<lList.size(); i++)
            System.out.println(i + " = " + s1[i]);

The array objects are:
0 = Isabella
1 = Angelina
2 = Pille
3 = Hazem

Throws NullPointerException if the specified array is null.

Community Additions

© 2015 Microsoft