Export (0) Print
Expand All

Collection.toArray Method (Object[ ])

Visual Studio 2005

Copies the elements of a Collection into specified array object.

Package: java.util

Assembly: vjslib (in vjslib.dll)


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

Parameters

arr

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

The array object that contains the Collection elements.

// col-toArr2.jsl
// Collection.toArray example

import java.util.*;

public class MyClass
{
    public static void main(String[] args)
    {
        // Construct a new Collection object:
        Collection lList =  new LinkedList();

        // Add some elements:
        lList.add("Isabella");
        lList.add("Angelina");
        lList.add("Pille");
        lList.add("Camelia");

        // 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]);
    }
}

/*
Output:
The array objects are:
0 = Isabella
1 = Angelina
2 = Pille
3 = Camelia
*/

Throws NullPointerException if the specified array is null.

Community Additions

ADD
Show:
© 2014 Microsoft