为什么Java List中的 toArray() 方法返回的类型不是和泛型一致

在使用 List中的 toArray()方法时,我们想当然的认为 toArray()返回的类型就应该和泛型一致的数组,而实际返回的却是Object数组。举个例子:定义 List<Integer> list = new ArrayList<>(); , list.toArrray() 返回的应该是 Integer[] 类型,而实际返回的却是 Object[] 。

查看List接口的源码,方法的申明如下:

/**
     * Returns an array containing all of the elements in this list in proper
     * sequence (from first to last element).
     *
     * <p>The returned array will be "safe" in that no references to it are
     * maintained by this list.  (In other words, this method must
     * allocate a new array even if this list 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 list in proper
     *         sequence
     * @see Arrays#asList(Object[])
     */
    Object[] toArray();

    /**
     * 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.
     *
     * <p>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 <tt>null</tt>.
     * (This is useful in determining the length of the list <i>only</i> if
     * the caller knows that the list does not contain any null elements.)
     *
     * <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 list known to contain only strings.
     * The following code can be used to dump the list into a newly
     * allocated array of <tt>String</tt>:
     *
     * <pre>{@code
     *     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 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.
     * @return 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
     * @throws NullPointerException if the specified array is null
     */
    <T> T[] toArray(T[] a);
View Code

从源码注释中得知,toArray()方法返回的是一个数组引用,该引用与在List中所维护数组的引用不同。即 toArray()创建了一个全新的数组并返回。而泛型无法通过类似于 new E[] 的语法创建,所以只能让返回的类型为 Object[] ,调用者可以根据情况对Object进行类型强转。

示例代码:

List<Integer> integers = Arrays.asList(1000, 1002, 1003);
Integer[] arr = (Integer[]) integers.toArray();

同时,为了使toArray()返回我们指定的数组引用,List还提供了方法 <T> T[] toArray(T[] a); ,在调用该方法的时候将List中的元素放到传入的参数a中。传入的a的长度可以是任意值:

若 a.length > list.size() ,则将list中的元素逐个放到a中,并将a的第list.size()+1个元素赋值为null,即 a[list.size()] = null;返回a

若 a.length = list.size(),则直接将list中的元素直接放到a中,返回a

若 a.length < list.size(),则创建一个长度为list.size()的数组,再将list中的元素放到该数组中并返回。此时返回的数组的引用和传入的引用不一致

示例代码:

List<Integer> integers = Arrays.asList(1000, 1002, 1003);

Integer[] arr = new Integer[integers.size()]; // 传入数组长度 = integers.size()
System.out.println(arr == integers.toArray(arr)); // true

arr = new Integer[integers.size() + 1]; // 传入数组长度 > integers.size()
System.out.println(arr == integers.toArray(arr));   // true

arr = new Integer[0]; // 传入数组长度 < integers.size()
System.out.println(arr == integers.toArray(arr));   // false

猜你喜欢

转载自www.cnblogs.com/robothy/p/12644053.html