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 11 Java/Android
Java集成swagger文档组件
Jun 28 Java/Android
解决MultipartFile.transferTo(dest) 报FileNotFoundExcep的问题
Jul 01 Java/Android
gateway网关接口请求的校验方式
Jul 15 Java/Android
mybatis 获取无数据的字段不显示的问题
Jul 15 Java/Android
使用springMVC所需要的pom配置
Sep 15 Java/Android
spring cloud 配置中心客户端启动遇到的问题
Sep 25 Java/Android
SSM项目使用拦截器实现登录验证功能
Jan 22 Java/Android
Flutter集成高德地图并添加自定义Maker的实践
Apr 07 Java/Android
解决Springboot PostMapping无法获取数据的问题
May 06 Java/Android
SpringBoot深入分析讲解监听器模式下
Jul 15 Java/Android
Mybatis 一级缓存和二级缓存原理区别
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
PHPWind与Discuz截取字符函数substrs与cutstr性能比较
2011/12/05 PHP
php微信公众平台开发类实例
2015/04/01 PHP
php curl 模拟登录并获取数据实例详解
2016/12/22 PHP
Laravel中日期时间处理包Carbon的简单使用
2017/09/21 PHP
LazyLoad 延迟加载(按需加载)
2010/05/31 Javascript
打印json对象的内容及JSON.stringify函数应用
2013/03/29 Javascript
JavaScript实现的GBK、UTF8字符串实际长度计算函数
2014/08/27 Javascript
IE浏览器不支持getElementsByClassName的解决方法
2014/08/27 Javascript
JS实现兼容各种浏览器的获取选择文本的方法【测试可用】
2016/06/21 Javascript
最简单纯JavaScript实现Tab标签页切换的方式(推荐)
2016/07/25 Javascript
扩展jquery easyui tree的搜索树节点方法(推荐)
2016/10/28 Javascript
Bootstrap CSS组件之分页(pagination)和翻页(pager)
2016/12/17 Javascript
js时间戳和c#时间戳互转方法(推荐)
2017/02/15 Javascript
jquery实现一个全局计时器(商城可用)
2017/06/30 jQuery
Bootstrap 树控件使用经验分享(图文解说)
2017/11/06 Javascript
说说如何在Vue.js中实现数字输入组件的方法
2019/01/08 Javascript
vue自定义js图片碎片轮播图切换效果的实现代码
2019/04/28 Javascript
微信小程序实现手势滑动效果
2019/08/26 Javascript
微信小程序去除左上角返回键的实现方法
2020/03/06 Javascript
Python黑魔法Descriptor描述符的实例解析
2016/06/02 Python
Pandas GroupBy对象 索引与迭代方法
2018/11/16 Python
python 用下标截取字符串的实例
2018/12/25 Python
解决windows上安装tensorflow时报错,“DLL load failed: 找不到指定的模块”的问题
2020/05/20 Python
Django REST 异常处理详解
2020/07/15 Python
Django中如何用xlwt生成表格的方法步骤
2021/01/31 Python
HTML5 画布canvas使用方法
2016/03/18 HTML / CSS
模具毕业生推荐信
2014/02/15 职场文书
《秋姑娘的信》教学反思
2014/02/28 职场文书
80后职场人的职业生涯规划
2014/03/08 职场文书
2014年端午节活动方案
2014/03/11 职场文书
企业精细化管理实施方案
2014/03/23 职场文书
贯彻落实“八项规定”思想汇报
2014/09/13 职场文书
中秋节寄语2015
2015/03/24 职场文书
小学安全工作总结2015
2015/05/18 职场文书
2015年小学生国庆节演讲稿
2015/07/30 职场文书
直播实况, OMG破敌三路五十分钟大战神技局摩托车
2022/04/01 DOTA