List.toArray

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值