Collection的toArray()方法和toArray(T[] a)方法源码解析

前言

toArray()方法和toArray(T[] a)方法是Collection接口中的两个将集合转换为数组的方法,那么这两个方法有什么异同点呢?

1.toArray()源码

   /**
     * Returns an array containing all of the elements in this collection.
     * If this collection makes any guarantees as to what order its elements
     * are returned by its iterator, this method must return the elements in
     * the same order.
     *
     * <p>The returned array will be "safe" in that no references to it are
     * maintained by this collection.  (In other words, this method must
     * allocate a new array even if this collection is backed by an array).
     * The caller is thus free to modify the returned array.
     *
     * <p>This method acts as bridge between array-based and collection-based
     * APIs.
     *
     * @return an array containing all of the elements in this collection
     */
    Object[] toArray();

翻译:

返回一个包含集合所有元素的Object数组,如果这个集合保证了它的迭代器返回元素的顺序,那么此方法返回的元素的顺序必须也是同样的顺序。

这个方法返回的数组将会是安全的,因为这个集合中没有任何指向这个数组的引用。换句话说,这个方法必须分配一个新的数组来保存集合的元素,即使这个集合本身就是数组类型的。

这个方法充当基于数组和基于集合的api之间的桥梁。

2.toArray(T[] a)源码

    /**
     * Returns an array containing all of the elements in this collection;
     * the runtime type of the returned array is that of the specified array.
     * If the collection 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 collection.
     *
     * <p>If this collection fits in the specified array with room to spare
     * (i.e., the array has more elements than this collection), the element
     * in the array immediately following the end of the collection is set to
     * <tt>null</tt>.  (This is useful in determining the length of this
     * collection <i>only</i> if the caller knows that this collection does
     * not contain any <tt>null</tt> elements.)
     *
     * <p>If this collection makes any guarantees as to what order its elements
     * are returned by its iterator, this method must return the elements in
     * the same order.
     *
     * <p>Like the {@link #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.
     *
     * <p>Suppose <tt>x</tt> is a collection known to contain only strings.
     * The following code can be used to dump the collection into a newly
     * allocated array of <tt>String</tt>:
     *
     * <pre>
     *     String[] y = x.toArray(new String[0]);</pre>
     *
     * Note that <tt>toArray(new Object[0])</tt> is identical in function to
     * <tt>toArray()</tt>.
     *
     * @param <T> the runtime type of the array to contain the collection
     * @param a the array into which the elements of this collection are to be
     *        stored, if it is big enough; otherwise, a new array of the same
     *        runtime type is allocated for this purpose.
     * @return an array containing all of the elements in this collection
     * @throws ArrayStoreException if the runtime type of the specified array
     *         is not a supertype of the runtime type of every element in
     *         this collection
     * @throws NullPointerException if the specified array is null
     */
    <T> T[] toArray(T[] a);

翻译:

返回一个包含集合所有元素的数组,被返回的数组的类型和参数中指定的类型是一致的。如果集合的长度适合指定的数组(即指定的数组可以容纳完集合的所有元素,或者说指定的数组的长度不小于集合的长度),将集合中的对象拷贝到指定的数组中,然后返回该数组,否则(即指定的数组的长度小于集合的长度),将集合中的对象拷贝到指定的数组中,然后返回一个包含所有集合元素的新数组,新数组的类型和参数数组的类型一致,数组长度和集合大小一致。

如果集合的元素全部放入数组中后,数组还有空余的空间(即集合的长度小于数组的长度),则紧跟数组最后一个元素的数组项将被赋为null(仅在调用者知道集合中不含null元素的情况下获取集合长度的时候很有用)。

如果这个集合保证了它的迭代器返回元素的顺序,那么此方法返回的元素的顺序必须也是同样的顺序。

类似toArray()方法,此方法充当基于数组和基于集合的api之间的桥梁。进一步说,这个方法允许精确控制返回数组的运行时类型,并且在某些特定的情况下(即指定数组的长度不小于集合的长度的时候),用来节省内存分配开销。

假定x是一个仅包含String类型元素的集合,下面的代码可以将集合的元素拷贝到一个新的数组中当中去

String[] y = x.toArray(new String[0]);

需要注意的是toArray(new Object[0])和toArray()产生的效果是一模一样的

toArray(T[] a)最大的特点就是:

a.当参数数组的长度不小于集合中元素的个数的时候,将集合中的元素按顺序拷贝到参数数组中,并且如果参数数组的长度大于集合中元素的个数的时候,接跟着拷贝后最后一个元素的数组项会被设置为null(参数数组中的数据项是发生了改变了的).

b.当参数数组的长度小于集合中元素的个数的时候,重新分配一个长度和集合中元素的个数相同的新数组,然后将集合中的元素全部拷贝到这个新数组中,并返回这个新数组,新数组的类型和参数类型是一致的(参数数组中的数据项是没有任何改变的)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值