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并发编程之详解CyclicBarrier线程同步
Jun 23 Java/Android
详解Java线程池是如何重复利用空闲线程的
Jun 26 Java/Android
如何给HttpServletRequest增加消息头
Jun 30 Java/Android
解决MultipartFile.transferTo(dest) 报FileNotFoundExcep的问题
Jul 01 Java/Android
springboot+WebMagic+MyBatis爬虫框架的使用
Aug 07 Java/Android
Java获取e.printStackTrace()打印的信息方式
Aug 07 Java/Android
OpenCV实现普通阈值
Nov 17 Java/Android
深入浅出讲解Java8函数式编程
Jan 18 Java/Android
使用Java去实现超市会员管理系统
Mar 18 Java/Android
springboot入门 之profile设置方式
Apr 04 Java/Android
Android在Sqlite3中的应用及多线程使用数据库的建议
Apr 24 Java/Android
基于Android10渲染Surface的创建过程
Aug 14 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制作新闻系统的思路
2006/10/09 PHP
PHP 根据IP地址控制访问的代码
2010/04/22 PHP
php+jQuery实现的三级导航栏下拉菜单显示效果
2017/08/10 PHP
juqery 学习之五 文档处理 包裹、替换、删除、复制
2011/02/11 Javascript
JQuery优缺点分析说明
2011/04/10 Javascript
javascript 弹出层组件(升级版)
2011/05/12 Javascript
jquery查找父元素、子元素(个人经验总结)
2014/04/09 Javascript
微信公众平台开发教程(四) 实例入门:机器人回复(附源码)
2016/12/02 Javascript
JavaScript实现Fly Bird小游戏
2016/12/15 Javascript
vue2.0 如何把子组件的数据传给父组件(推荐)
2018/01/15 Javascript
JavaScript使用math.js进行精确计算操作示例
2018/06/19 Javascript
微信小程序 MinUI组件库系列之badge徽章组件示例
2018/08/20 Javascript
javaScript实现游戏倒计时功能
2018/11/17 Javascript
基于NodeJS开发钉钉回调接口实现AES-CBC加解密
2020/08/20 NodeJs
node中短信api实现验证码登录的示例代码
2021/01/20 Javascript
让python json encode datetime类型
2010/12/28 Python
python利用拉链法实现字典方法示例
2017/03/25 Python
Python实现解析Bit Torrent种子文件内容的方法
2017/08/29 Python
python爬虫基本知识
2018/03/05 Python
Python 单元测试(unittest)的使用小结
2018/11/14 Python
Python采集猫眼两万条数据 对《无名之辈》影评进行分析
2018/12/05 Python
Python3.5内置模块之time与datetime模块用法实例分析
2019/04/27 Python
Python类反射机制使用实例解析
2019/12/30 Python
Python猜数字算法题详解
2020/03/01 Python
pycharm2020.1.2永久破解激活教程,实测有效
2020/10/29 Python
微软开源最强Python自动化神器Playwright(不用写一行代码)
2021/01/05 Python
详解pandas映射与数据转换
2021/01/22 Python
使用numpngw和matplotlib生成png动画的示例代码
2021/01/24 Python
HTML5实现动画效果的方式汇总
2016/02/29 HTML / CSS
消防安全汇报材料
2014/02/08 职场文书
竞聘上岗演讲稿
2014/05/16 职场文书
抗震救灾标语
2014/06/26 职场文书
2014年保险公司工作总结
2014/11/22 职场文书
司机个人年终总结
2015/03/03 职场文书
舌尖上的中国观后感
2015/06/02 职场文书
监守自盗观后感
2015/06/10 职场文书