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