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 相关文章推荐
Java如何实现树的同构?
Jun 22 Java/Android
基于Java的MathML转图片的方法(示例代码)
Jun 23 Java/Android
详解Java分布式事务的 6 种解决方案
Jun 26 Java/Android
spring项目中切面及AOP的使用方法
Jun 26 Java/Android
Java SSH 秘钥连接mysql数据库的方法
Jun 28 Java/Android
详细了解MVC+proxy
Jul 09 Java/Android
SpringBoot实现quartz定时任务可视化管理功能
Aug 30 Java/Android
Java使用Unsafe类的示例详解
Sep 25 Java/Android
Java 使用类型为Object的变量指向任意类型的对象
Apr 13 Java/Android
Java后端 Dubbo retries 超时重试机制的解决方案
Apr 14 Java/Android
mybatis-plus模糊查询指定字段
Apr 28 Java/Android
Spring boot admin 服务监控利器详解
Aug 05 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+MYSQL的文章管理系统(一)
2006/10/09 PHP
php 随机生成10位字符代码
2009/03/26 PHP
php中使用Akismet防止垃圾评论的代码
2011/06/10 PHP
PHP 杂谈《重构-改善既有代码的设计》之一 重新组织你的函数
2012/04/09 PHP
PHP使用PDO抽象层获取查询结果的方法示例
2018/05/10 PHP
Ubuntu中支持PHP5与PHP7双版本的简单实现
2018/08/19 PHP
Autocomplete Textbox Example javascript实现自动完成成功
2007/08/17 Javascript
IE下window.onresize 多次调用与死循环bug处理方法介绍
2013/11/12 Javascript
JS+CSS 制作的超级简单的下拉菜单附图
2013/11/22 Javascript
javaScript的函数对象的声明详解
2015/02/06 Javascript
JS判断日期格式是否合法的简单实例
2016/07/11 Javascript
详解webpack异步加载业务模块
2017/06/23 Javascript
Angular实现的简单定时器功能示例
2017/12/28 Javascript
jQuery+CSS实现的标签页效果示例【测试可用】
2018/08/14 jQuery
JS Object.preventExtensions(),Object.seal()与Object.freeze()用法实例分析
2018/08/25 Javascript
seajs和requirejs模块化简单案例分析
2019/08/26 Javascript
[05:28]刀塔密之一:团结则存
2014/07/03 DOTA
在Python的Django框架中包装视图函数
2015/07/20 Python
举例讲解Python中的死锁、可重入锁和互斥锁
2015/11/05 Python
Python标准库inspect的具体使用方法
2017/12/06 Python
快速了解Python相对导入
2018/01/12 Python
如何在python中写hive脚本
2019/11/08 Python
Django中template for如何使用方法
2021/01/31 Python
html5中如何将图片的绝对路径转换成文件对象
2018/01/11 HTML / CSS
澳大利亚著名的纺织品品牌:Canningvale
2020/05/05 全球购物
农村婚礼证婚词
2014/01/10 职场文书
函授大学生自我鉴定
2014/02/05 职场文书
三八妇女节活动总结
2014/05/04 职场文书
员工生日活动方案
2014/08/24 职场文书
2015年毕业生个人自荐书
2015/03/24 职场文书
张思德观后感
2015/06/09 职场文书
2016年万圣节活动个人总结
2016/04/05 职场文书
Goland使用Go Modules创建/管理项目的操作
2021/05/06 Golang
MySQL详解进行JDBC编程与增删改查方法
2022/06/16 MySQL
Go微服务项目配置文件的定义和读取示例详解
2022/06/21 Golang
Mysql中的触发器定义及语法介绍
2022/06/25 MySQL