Java 数组内置函数toArray详解

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解析内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 解决Laravel5.5下的toArray问题

    作为一个有轻度强迫症且受ThinkPHP影响较深的PHP码农,总觉得Laravel5.5的DB::xxoo->get()->toArray()之后竟然还没得到我想要的ThinkPHP中的select()出来的数组,于是决定做一下修改. PS:出于尽量不影响原有框架的考虑,我是新建了一个方法叫getList来暂代toArray那不知所谓的返回结果,在没有找到更好的解决办法之前,暂时这么用着,总体感觉,get()->getList()仍觉得略丑.... 具体修改如下: 1.在/vendor/

  • 关于List.ToArray()方法的效率测试

    之前一直认为因为List内部实现是数组,ToArray的实现只是将数组返回出去而已. 今天测了一下发现并不是那样 var a = new List<int>(); for (int i = 0; i < 10000; i++) { a.Add(i); } DebugHelper.StartWatch(); foreach (var i in Enumerable.Range(0, 10000)) { a.ToArray(); } DebugHelper.StopWatch(); 1万数量

  • jQuery学习笔记之toArray()

    从源码中可以看到,这些常见的方法,都是直接从原生的javascript中"借鉴"过来的.为什么这么说呢?225行中,在运行时,this是指向jQuery对象的,通过call语句借鉴slice方法,切割出一个数组出来,这种"借鸡下蛋"的方法,真的是让人拍手叫绝.slice(start,end)是用来切分数组的.start表示切分数组的开始位置.省略end表示到从start一直到最后

  • Java ArrayList.toArray(T[]) 方法的参数类型是 T 而不是 E的原因分析

    前两天给同事做 code review,感觉自己对 Java 的 Generics 掌握得不够好,便拿出 <Effective Java>1 这本书再看看相关的章节.在 Item 24:Eliminate unchecked warnings 这一节中,作者拿 ArrayList 类中的 public <T> T[] toArray(T[] a) 方法作为例子来说明如何对变量使用 @SuppressWarnings annotation. ArrayList 是一个 generic

  • Java 数组内置函数toArray详解

    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

  • 六个Python编程最受用的内置函数使用详解

    目录 1. Map 函数 2. Lamdba 函数 3. Enumerate 函数 4. Reduce 函数 5. Filter 函数 6. Zip 函数 在日常的python编程中使用这几个函数来简化我们的编程工作,经常使用能使编程效率大大地提高. 1. Map 函数 map函数可以使用另外一个函数转换整个可迭代对象的函数,包括将字符串转换为数字.数字的四舍五入等等. 之所以使用map函数来完成这些事情可以节约内存,使代码的运行速度提高,并且使用的代码量比较少. 比如这里需要将一个字符串的数组

  • Python内置函数dir详解

    1.命令介绍 最近学习并使用了一个python的内置函数dir,首先help一下: 复制代码 代码如下: >>> help(dir) Help on built-in function dir in module __builtin__: dir()     dir([object]) -> list of strings Return an alphabetized list of names comprising (some of) the attributes     of

  • python 内置函数汇总详解

    1.强制类型转换 dict() 强制转换为字典类型 list() 强制转换为列表类型 tuple() 强制转换为元组类型 int() 强制转为整形 str() 强制转换为字符串类型 bool() 强制转换为布尔类型 set() 强制转换为集合类型 2.输入输出 print() 输出 input() 输入 3.数学相关 abs() 绝对值 qqq = abs(-253) print(qqq) float() 转换成浮点型 v = 55 v1 = float(v) print(v1) max() 找

  • python内置函数zip详解

    目录 一.简介 二.详解 三.代码 四.Reference 总结 一.简介 zip() 函数用于将可迭代的对象作为参数,主要功能是将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表. 如果各个iterable迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表. 要点:打包成元组,返回列表,如果长度不一致,则与短的iterable对齐 二.详解 语法:zip([iterable, ...]) 参数:iterable是一个或者多个可以迭代的

  • Python内置函数OCT详解

    英文文档: 复制代码 代码如下: oct ( x ) Convert an integer number to an octal string. The result is a valid Python expression. If x is not a Pythonobject, it has to define anmethod that returns an integer. 说明: 1. 函数功能将一个整数转换成8进制字符串.如果传入浮点数或者字符串均会报错. >>> a = o

  • Python 内置函数complex详解

    英文文档: class complex([real[, imag]]) Return a complex number with the value real + imag*1j or convert a string or number to a complex number. If the first parameter is a string, it will be interpreted as a complex number and the function must be calle

  • Java多线程之显示锁和内置锁总结详解

    总结多线程之显示锁和内置锁 Java中具有通过Synchronized实现的内置锁,和ReentrantLock实现的显示锁,这两种锁各有各的好处,算是互有补充,这篇文章就是做一个总结. *Synchronized* 内置锁获得锁和释放锁是隐式的,进入synchronized修饰的代码就获得锁,走出相应的代码就释放锁. synchronized(list){ //获得锁 list.append(); list.count(); }//释放锁 通信 与Synchronized配套使用的通信方法通常

  • asp 内置对象 Application 详解

    asp内置对象 Application 详解  在 ASP 的内建对象中除了用于发送.接收和处理数据的对象外,还有一些非常实用的代表 Active Server 应用程序和单个用户信息的对象.  让我们先来看看 Application 对象.在同一虚拟目录及其子目录下的所有 .asp 文件构成了 ASP 应用程序.我们非但可以使用 Application 对象,在给定的应用程序的所有用户之间共享信息,并在服务器运行期间持久的保存数据.而且,Application 对象还有控制访问应用层数据的方法

  • python字符串string的内置方法实例详解

    下面给大家分享python 字符串string的内置方法,具体内容详情如下所示: #__author: "Pizer Wang" #__date: 2018/1/28 a = "Let's go" print(a) print("-------------------") a = 'Let\'s go' print(a) print("-------------------") print("hello"

随机推荐