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 相关文章推荐
实体类或对象序列化时,忽略为空属性的操作
Jun 30 Java/Android
Java 在生活中的 10 大应用
Nov 02 Java/Android
Java中try catch处理异常示例
Dec 06 Java/Android
关于MybatisPlus配置双数据库驱动连接数据库问题
Jan 22 Java/Android
正则表达式拆分url实例代码
Feb 24 Java/Android
Java并发编程之原子性-Atomic的使用
Mar 16 Java/Android
spring cloud eureka 服务启动失败的原因分析及解决方法
Mar 17 Java/Android
教你在 Java 中实现 Dijkstra 最短路算法的方法
Apr 08 Java/Android
Elasticsearch 配置详解
Apr 19 Java/Android
Java Spring Boot 正确读取配置文件中的属性的值
Apr 20 Java/Android
Java中的Kafka为什么性能这么快及4大核心详析
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初学者的8点有效建议
2010/11/20 PHP
使用JSON实现数据的跨域传输的php代码
2011/12/20 PHP
php计算指定目录下文件占用空间的方法
2015/03/13 PHP
php中动态变量用法实例
2015/06/10 PHP
详谈 Jquery Ajax异步处理Json数据.
2011/09/09 Javascript
js实现屏蔽默认快捷键调用自定义事件示例
2013/06/18 Javascript
JS冒泡事件的快速解决方法
2013/12/16 Javascript
js中函数声明与函数表达式
2015/06/03 Javascript
解决Node.js使用MySQL出现connect ECONNREFUSED 127.0.0.1:3306的问题
2017/03/09 Javascript
AngularJS select设置默认值的实现方法
2017/08/25 Javascript
详解React项目如何修改打包地址(编译输出文件地址)
2019/03/21 Javascript
微信小程序 image组件遇到的问题
2019/05/28 Javascript
Vue+Vuex实现自动登录的知识点详解
2020/03/04 Javascript
Python 命令行非阻塞输入的小例子
2013/09/27 Python
使用graphics.py实现2048小游戏
2015/03/10 Python
python 计算文件的md5值实例
2017/01/13 Python
python使用for循环计算0-100的整数的和方法
2019/02/01 Python
基于Python函数和变量名解析
2019/07/19 Python
python使用docx模块读写docx文件的方法与docx模块常用方法详解
2020/02/17 Python
Python如何使用队列方式实现多线程爬虫
2020/05/12 Python
python给视频添加背景音乐并改变音量的具体方法
2020/07/19 Python
印度第一网上礼品店:IGP.com
2020/02/06 全球购物
学前教育专业毕业生自荐信
2013/10/03 职场文书
信息专业大学生自我评价分享
2014/01/17 职场文书
《乞巧》教学反思
2014/02/27 职场文书
幼儿园儿童节主持词
2014/03/21 职场文书
小学生作文评语大全
2014/04/21 职场文书
2014年劳动部工作总结
2014/12/11 职场文书
银行招聘自荐信
2015/03/06 职场文书
家长对学校的意见和建议
2015/06/03 职场文书
捐款仪式主持词
2015/07/04 职场文书
2016年国庆节67周年活动总结
2016/04/01 职场文书
大学生创业计划书常用模板
2019/08/07 职场文书
详解Java分布式事务的 6 种解决方案
2021/06/26 Java/Android
浅谈sql_@SelectProvider及使用注意说明
2021/08/04 Java/Android
使用 Apache 反向代理的设置技巧
2022/01/18 Servers