前言
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()产生的效果是一模一样的。