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 相关文章推荐
springboot @ConfigurationProperties和@PropertySource的区别
Jun 11 Java/Android
详解java如何集成swagger组件
Jun 21 Java/Android
详解Java实现设计模式之责任链模式
Jun 23 Java/Android
Java实现聊天机器人完善版
Jul 04 Java/Android
springboot 启动如何排除某些bean的注入
Aug 02 Java/Android
详解JAVA的控制语句
Nov 11 Java/Android
springmvc直接不经过controller访问WEB-INF中的页面问题
Feb 24 Java/Android
springboot入门 之profile设置方式
Apr 04 Java/Android
零基础学java之方法的定义与调用详解
Apr 10 Java/Android
springboot读取nacos配置文件
May 20 Java/Android
Android开发EditText禁止输入监听及InputFilter字符过滤
Jun 10 Java/Android
AndroidStudio图片压缩工具ImgCompressPlugin使用实例
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解析html类库simple_html_dom的转码bug
2014/05/22 PHP
解析WordPress中的post_class与get_post_class函数
2016/01/04 PHP
PHP读取大文件的几种方法介绍
2016/10/27 PHP
PHPCMS手机站伪静态设置详细教程
2017/02/06 PHP
kindeditor 加入七牛云上传的实例讲解
2017/11/12 PHP
用Javascript同时提交多个Web表单的方法
2009/12/26 Javascript
关于文本框的一些限制控制总结~~
2010/04/15 Javascript
js document.write()使用介绍
2014/02/21 Javascript
js简单工厂模式用法实例
2015/06/30 Javascript
Javascript iframe交互并兼容各种浏览器的解决方法
2016/07/12 Javascript
Google Maps基础及实例解析
2016/08/06 Javascript
JS实现商品筛选功能
2020/08/19 Javascript
Web技术实现移动监测的介绍
2017/09/18 Javascript
利用node实现一个批量重命名文件的函数
2017/12/21 Javascript
vue2.0路由切换后页面滚动位置不变BUG的解决方法
2018/03/14 Javascript
Nodejs让异步变成同步的方法
2019/03/02 NodeJs
Node.js 实现简单的无侵入式缓存框架的方法
2019/07/21 Javascript
layui switch 开关监听 弹出确定状态转换的例子
2019/09/21 Javascript
vue 使用post/get 下载导出文件操作
2020/08/07 Javascript
js实现有趣的倒计时效果
2021/01/19 Javascript
[43:57]Liquid vs Mineski 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/19 DOTA
python中实现迭代器(iterator)的方法示例
2017/01/19 Python
Python实现矩阵加法和乘法的方法分析
2017/12/19 Python
python使用numpy读取、保存txt数据的实例
2018/10/14 Python
pygame游戏之旅 游戏中添加显示文字
2018/11/20 Python
Python设计模式之原型模式实例详解
2019/01/18 Python
深入浅析Python 中 is 语法带来的误解
2019/05/07 Python
Python实现爬取亚马逊数据并打印出Excel文件操作示例
2019/05/16 Python
HTML5 Plus 实现手机APP拍照或相册选择图片上传功能
2016/07/13 HTML / CSS
日本著名化妆品零售网站:Cosme Land
2019/03/01 全球购物
奥地利时尚、美容、玩具和家居之家:Kastner & Öhler
2020/04/26 全球购物
构造方法和其他方法的区别
2016/04/26 面试题
同学聚会欢迎辞
2014/01/14 职场文书
教师自我剖析材料
2014/09/29 职场文书
民主评议党员总结
2014/10/20 职场文书
2014年房产销售工作总结
2014/12/08 职场文书