Java 数组内置函数toArray详解


Posted in Java/Android onJune 28, 2021

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详解的文章就介绍到这了,更多相关Java toArray解析内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Java/Android 相关文章推荐
解决Maven项目中 Invalid bound statement 无效的绑定问题
Jun 15 Java/Android
JVM入门之类加载与字节码技术(类加载与类的加载器)
Jun 15 Java/Android
java中重写父类方法加不加@Override详解
Jun 21 Java/Android
Java图书管理系统,课程设计必用(源码+文档)
Jun 30 Java/Android
Springboot配置suffix指定mvc视图的后缀方法
Jul 03 Java/Android
SpringCloud Function SpEL注入漏洞分析及环境搭建
Apr 08 Java/Android
Java中的继承、多态以及封装
Apr 11 Java/Android
Java处理延时任务的常用几种解决方案
Jun 01 Java/Android
java实现面板之间切换功能
Jun 10 Java/Android
SpringBoot使用AOP实现统计全局接口访问次数详解
Jun 16 Java/Android
基于Android10渲染Surface的创建过程
Aug 14 Java/Android
Java实现贪吃蛇游戏的示例代码
Sep 23 Java/Android
Java集成swagger文档组件
死磕 java同步系列之synchronized解析
Jun 28 #Java/Android
利用Java设置Word文本框中的文字旋转方向的实现方法
Springboot集成阿里云OSS上传文件系统教程
简单总结SpringMVC拦截器的使用方法
SpringBoot实现异步事件驱动的方法
Jun 28 #Java/Android
Spring整合Mybatis的全过程
Jun 28 #Java/Android
You might like
PHP设计模式之结构模式的深入解析
2013/06/13 PHP
PHP获取数组长度或某个值出现次数的方法
2015/02/11 PHP
PHP实现将标点符号正则替换为空格的方法
2017/08/09 PHP
PHP Web表单生成器案例分析
2020/06/02 PHP
jquery一般方法介绍 入门参考
2011/06/21 Javascript
Google Map V3 绑定气泡窗口(infowindow)Dom事件实现代码
2013/04/26 Javascript
让浏览器DOM元素最后加载的js方法
2014/07/29 Javascript
JavaScript的作用域和块级作用域概念理解
2014/09/21 Javascript
初探nodeJS
2017/01/24 NodeJs
vue+ElementUI实现订单页动态添加产品数据效果实例代码
2017/07/13 Javascript
JS获取本地地址及天气的方法实例小结
2019/05/10 Javascript
微信公众号H5之微信分享常见错误和问题(小结)
2019/11/14 Javascript
微信小程序静默登录的实现代码
2020/01/08 Javascript
javascript 数组(list)添加/删除的实现
2020/12/17 Javascript
使用Python标准库中的wave模块绘制乐谱的简单教程
2015/03/30 Python
Python中使用装饰器来优化尾递归的示例
2016/06/18 Python
详解python中递归函数
2019/04/16 Python
python和mysql交互操作实例详解【基于pymysql库】
2019/06/04 Python
用pyqt5 给按钮设置图标和css样式的方法
2019/06/24 Python
使用OpCode绕过Python沙箱的方法详解
2019/09/03 Python
Python numpy数组转置与轴变换
2019/11/15 Python
python 元组的使用方法
2020/06/09 Python
python中random模块详解
2021/03/01 Python
css3中用animation的steps属性制作帧动画
2019/04/25 HTML / CSS
ziaja齐叶雅官方海外旗舰店:来自波兰的天然护肤品牌
2017/01/02 全球购物
门前三包责任书
2014/04/15 职场文书
同居协议书范本
2014/04/23 职场文书
婚内房产协议书范本
2014/10/02 职场文书
纪委书记群众路线整改措施思想汇报
2014/10/09 职场文书
机关作风建设自查报告及整改措施
2014/10/21 职场文书
夫妻分居协议书范本(有子女版)
2014/11/01 职场文书
少先队中队工作总结
2015/08/14 职场文书
2019最新激励员工口号大全!
2019/06/28 职场文书
助学金申请书该怎么写?
2019/07/16 职场文书
完美处理python与anaconda环境变量的冲突问题
2021/04/07 Python
用CSS3画一个爱心
2021/04/27 HTML / CSS