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实现
Jun 23 Java/Android
JavaWeb 入门:Hello Servlet
Jul 16 Java/Android
java中用float时,数字后面加f,这样是为什么你知道吗
Sep 04 Java/Android
JAVA API 实用类 String详解
Oct 05 Java/Android
深入解读Java三大集合之map list set的用法
Nov 11 Java/Android
详解Spring Security中的HttpBasic登录验证模式
Mar 17 Java/Android
Spring Boot DevTools 全局配置学习指南
Mar 31 Java/Android
Spring 使用注解开发
May 20 Java/Android
Spring Security动态权限的实现方法详解
Jun 16 Java/Android
springboot+rabbitmq实现智能家居实例详解
Jul 23 Java/Android
app场景下uniapp的扫码记录
Jul 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 GeoIP的使用教程
2011/03/09 PHP
无JS,完全php面向过程数据分页实现代码
2012/08/27 PHP
php实现天干地支计算器示例
2014/03/14 PHP
phpMyAdmin自动登录和取消自动登录的配置方法
2014/05/12 PHP
学习php设计模式 php实现观察者模式(Observer)
2015/12/09 PHP
解密效果
2006/06/23 Javascript
Javascript hasOwnProperty 方法 &amp; in 关键字
2008/11/26 Javascript
javascript this用法小结
2008/12/19 Javascript
THREE.JS入门教程(5)你应当知道的十件事
2013/01/24 Javascript
自动设置iframe大小的jQuery代码
2013/09/11 Javascript
JavaScript中的原型链prototype介绍
2014/12/30 Javascript
jQuery插件StickUp实现网页导航置顶
2015/04/12 Javascript
js实现tab切换效果实例
2015/09/16 Javascript
bootstrap 模态框(modal)实现水平垂直居中显示
2017/01/23 Javascript
JavaScript实现的浏览器下载文件的方法
2017/08/09 Javascript
实例详解JSON取值(key是中文或者数字)方式
2017/08/24 Javascript
利用express启动一个server服务的方法
2017/09/17 Javascript
JavaScript寄生组合式继承原理与用法分析
2019/01/11 Javascript
highCharts提示框中显示当前时间的方法
2019/01/18 Javascript
JavaScript基础之this和箭头函数详析
2019/09/05 Javascript
p5.js临摹动态图形的方法
2019/10/23 Javascript
javascript实现异形滚动轮播
2019/11/28 Javascript
Element中Slider滑块的具体使用
2020/07/29 Javascript
Nuxt pages下不同的页面对应layout下的页面布局操作
2020/11/05 Javascript
Python unittest discover批量执行代码实例
2020/09/08 Python
详解Django ORM引发的数据库N+1性能问题
2020/10/12 Python
总结html5自定义属性有哪些
2020/04/01 HTML / CSS
任意存:BOXFUL
2018/05/21 全球购物
C#里面可以避免一个类被其他类继承么?如何?
2013/09/26 面试题
应届生体育教师自荐信
2013/10/03 职场文书
财务人员的自我评价范文
2014/03/03 职场文书
股东授权委托书范本
2014/09/13 职场文书
大学生毕业个人总结
2015/02/15 职场文书
JavaScript实现复选框全选功能
2021/04/11 Javascript
Python实现PIL图像处理库绘制国际象棋棋盘
2021/07/16 Python
人民币符号
2022/02/17 杂记