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.

`list.toArray()` 是 Java 中 `List` 接口提供的方法,用于将列表转换为数组。该方法有两个重载版本: ### toArray() 此方法返回一个包含列表中所有元素的 `Object` 类型数组,其作用是将 `elementData` 数组中的元素拷贝到长度为 `size` 的 `Object` 数组中,并返回这个 `Object` 数组。 示例代码如下: ```java import java.util.ArrayList; import java.util.Arrays; public class ToArrayExample { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("你"); list.add("好"); Object[] arr = list.toArray(); System.out.println(Arrays.toString(arr)); } } ``` ### toArray(T[] a) 该方法将列表中的元素存储在指定类型的数组中,如果指定的数组大小足够容纳列表中的所有元素,则将其返回;否则,会创建一个具有相同运行时类型的新数组,并将列表中的元素复制到新数组中。 示例代码如下: ```java import java.util.ArrayList; import java.util.Arrays; public class ToArrayWithParameterExample { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("你"); list.add("好"); String[] arr = new String[list.size()]; String[] result = list.toArray(arr); System.out.println(Arrays.toString(result)); } } ``` ### 适用场景 - **与旧代码交互**:在一些旧的 Java 代码或者第三方库中,可能只接受数组作为参数,此时可以使用 `toArray()` 方法将 `List` 转换为数组。 - **性能优化**:在某些情况下,数组的访问速度可能比 `List` 更快。如果需要对数据进行频繁的随机访问,可以将 `List` 转换为数组。 - **数据处理**:在进行一些数据处理时,数组可能更方便操作,例如使用 `Arrays` 类提供的各种工具方法。 ### 注意事项 - `toArray()` 方法返回的是 `Object` 类型的数组,如果需要特定类型的数组,需要使用 `toArray(T[] a)` 方法。 - `toArray(T[] a)` 方法中,如果传入的数组长度小于列表的大小,会创建一个新的数组;如果传入的数组长度大于列表的大小,数组中多余的元素会被置为 `null`。 - 该方法只能用于存储对象类型的 `List`,不能用于存储基本类型的 `List`。如果需要存储基本类型,可以使用对应的包装类。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值