List.toArray Method (Object[ ])
Copies and returns all the elements of a List object into a specific array object.
Package: java.util
Assembly: vjslib (in vjslib.dll)
public abstract java.lang.Object[] toArray(
java.lang.Object[] arr);
Parameters
arr
The array that receives the elements of the List object.
Return Value
The array object that contains the elements of the List object.
Example
In this example, you create and initialize a linked list object, and then you convert the list to an array "s". Then you copy "s" to a new array "s1" and display the elements of "s1."
// list-toArr2.jsl
// List.toArray example
import java.util.*;
public class MyClass{
public static void main(String[] args){
// Create a linked list object:
LinkedList List = new LinkedList();
// Add some elements:
List.add("My node");
List.add("Her node");
List.add("Their node");
List.add("Your node");
// Create an array from the list:
String[] arr = new String[0];
// Copy lList to arr:
arr = (String[])List.toArray(arr);
// Display the new array elements:
System.out.println("The array elements are:");
for (int i=0; i<arr.length; i++){
System.out.println(i + "=" + arr[i]);
}
}
}
/*
Output:
The array elements are:
0=My node
1=Her node
2=Their node
3=Your node
*/
Remarks
If arr is not large enough to hold the List members it will be expanded.
The type of List elements should match the type of the array elements.