Java 数组内置函数toArray详解

 更新时间:2021年06月28日 16:21:44   作者:九童  
这篇文章主要介绍了Java 数组内置函数toArray详解,文本详细的讲解了toArray底层的代码和文档,需要的朋友可以参考下

java.util.List中的toArray函数

java.util.List<E> @NotNull 
public abstract <T> T[] toArray(@NotNull T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.
If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
Like the toArray() method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.
Suppose x is a list known to contain only strings. The following code can be used to dump the list into a newly allocated array of String:
 
     String[] y = x.toArray(new String[0]);
 
Note that toArray(new Object[0]) is identical in function to toArray().

Overrides:
toArray in interface Collection
Params:
a – the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Type parameters:
<T> – the runtime type of the array to contain the collection
Returns:
an array containing the elements of this list
Throws:
ArrayStoreException – if the runtime type of the specified array is not a supertype of the runtime type of every element in this list
NullPointerException – if the specified array is null
External annotations:
Abstract method toArray: @org.jetbrains.annotations.NotNull
Parameter a: @org.jetbrains.annotations.NotNull

翻译
java.util.List @NotNull

public abstract T[] toArray(@NotNull T[] a)

返回一个包含列表中所有元素的数组(从第一个元素到最后一个元素);返回数组的运行时类型是指定数组的运行时类型。如果列表适合指定的数组,则在其中返回该列表。否则,将使用指定数组的运行时类型和该列表的大小分配一个新数组。

如果列表适合指定的有空间的数组(即,数组的元素比列表的多),则紧挨着列表末尾的数组中的元素被设为null。(只有当调用者知道列表不包含任何空元素时,这在确定列表的长度时才有用。)

与toArray()方法一样,该方法充当基于数组和基于集合的api之间的桥梁。此外,这种方法允许精确控制输出数组的运行时类型,在某些情况下,可以用于节省分配成本。

假设x是一个只包含字符串的列表。下面的代码可以用来将列表转储到一个新分配的String数组中:

String[] y = x.toArray(new String[0]);

注意toArray(新对象[0])在函数中与toArray()相同。

覆盖:

toArray在接口集合

参数:

A -如果列表足够大,则存放列表中所有元素的数组;否则,将为此目的分配相同运行时类型的新数组。

类型参数:

-包含集合的数组的运行时类型

返回:

一个包含此列表元素的数组

抛出:

如果指定数组的运行时类型不是这个列表中每个元素的运行时类型的超类型,则会产生ArrayStoreException异常

NullPointerException -如果指定的数组为空

外部注释:

抽象方法:@org.jetbrains.annotations.NotNull

参数:@org.jetbrains.annotations.NotNull

public static void main(String[] args) {
    List<Double> asList = new ArrayList<Double>() {
        //使用匿名内部类来初始化。
        {
            add(35.6);
            add(3.2);
            add(90.);
        }
    };
    Double []sumVenderNumArray = new Double[]{333333.34,999.9,93.45,23.4,33.};
    Double [] sumVenderNumNum = asList.toArray(sumVenderNumArray);
    System.out.println(JSONObject.toJSONString(sumVenderNumNum));

}

运行结果:

在这里插入图片描述

到此这篇关于Java 数组内置函数toArray详解的文章就介绍到这了,更多相关Java toArray解析内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解Eclipse 字体、字号的设置、最佳字体推荐

    详解Eclipse 字体、字号的设置、最佳字体推荐

    这篇文章主要介绍了Eclipse 字体、字号的设置、最佳字体推荐,需要的朋友可以参考下
    2020-09-09
  • Scala常用List列表操作方法示例

    Scala常用List列表操作方法示例

    这篇文章主要介绍了Scala常用List列表操作方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06
  • java如何使用Lombok更优雅地编码

    java如何使用Lombok更优雅地编码

    Lombok能通过注解的方式,在编译时自动为属性生成构造器、getter/setter、equals、hashcode、toString方法。这样就省去了手动重建这些代码的麻烦,使代码看起来更简洁些。下面我们来详细学习下吧
    2019-06-06
  • java中JVM中如何存取数据和相关信息详解

    java中JVM中如何存取数据和相关信息详解

    这篇文章主要介绍了JVM中如何存取数据和相关信息详解,Java源代码文件(.java后缀)会被Java编译器编译为字节码文件,然后由JVM中的类加载器加载各个类的字节码文件,加载完毕之后,交由JVM执行引擎执行。JVM中怎么存取数据和相关信息呢?,需要的朋友可以参考下
    2019-06-06
  • SpringBoot常见错误图文总结

    SpringBoot常见错误图文总结

    最近在使用idea+Springboot开发项目中遇到一些问题,这篇文章主要给大家介绍了关于SpringBoot常见错误总结的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-06-06
  • Java编程Commons lang组件简介

    Java编程Commons lang组件简介

    这篇文章主要介绍了Java编程Commons lang组件的相关内容,十分具有参考意义,需要的朋友可以了解下。
    2017-09-09
  • Java多线程中断机制三种方法及示例

    Java多线程中断机制三种方法及示例

    这篇文章主要介绍了Java多线程中断机制三种方法及示例,向大家分享了这三种方法的介绍几代码示例,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • Java Apache common-pool对象池介绍

    Java Apache common-pool对象池介绍

    这篇文章主要介绍了Java Apache common-pool对象池介绍,文章通过围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下
    2022-09-09
  • 浅谈Java编程中的单例设计模式

    浅谈Java编程中的单例设计模式

    这篇文章主要介绍了Java编程中的单例设计模式,在许多语言的编程过程当中单例模式都被开发者们广泛采用,需要的朋友可以参考下
    2015-07-07
  • Java API如何实现向Hive批量导入数据

    Java API如何实现向Hive批量导入数据

    这篇文章主要介绍了Java API如何实现向Hive批量导入数据的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07

最新评论