zoukankan      html  css  js  c++  java
  • Java List <T> T[] toArray(T[] a) implementation

    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.

    This means that the programmer is in control over what type of array it should be.

    For example, for your ArrayList<Integer> instead of an Integer[] array you might want a Number[] or Object[] array.

    Furthermore, the method also checks the array that is passed in. If you pass in an array that has enough space for all elements, the the toArray 

    method re-uses that array. This means:

    Integer[] myArray = new Integer[myList.size()];
    myList.toArray(myArray);

    or

    Integer[] myArray = myList.toArray(new Integer[myList.size()]);

    has the same effect but is more efficient than

    Integer[] myArray = myList.toArray(new Integer[0]);

    as the latter operation uses reflection to check the array type and then dynamically construct an array of the right type. By passing in a correctly

    sized array in the first place, reflection does not have to be used to allocate a new array inside the toArray method.

        /**
         * {@inheritDoc}
         *
         * <p>This implementation returns an array containing all the elements
         * returned by this collection's iterator in the same order, stored in
         * consecutive elements of the array, starting with index {@code 0}.
         * If the number of elements returned by the iterator is too large to
         * fit into the specified array, then the elements are returned in a
         * newly allocated array with length equal to the number of elements
         * returned by the iterator, even if the size of this collection
         * changes during iteration, as might happen if the collection permits
         * concurrent modification during iteration.  The {@code size} method is
         * called only as an optimization hint; the correct result is returned
         * even if the iterator returns a different number of elements.
         *
         * <p>This method is equivalent to:
         *
         *  <pre> {@code
         * List<E> list = new ArrayList<E>(size());
         * for (E e : this)
         *     list.add(e);
         * return list.toArray(a);
         * }</pre>
         *
         * @throws ArrayStoreException  {@inheritDoc}
         * @throws NullPointerException {@inheritDoc}
         */
        @SuppressWarnings("unchecked")
        public <T> T[] toArray(T[] a) {
            // Estimate size of array; be prepared to see more or fewer elements
            int size = size();
            T[] r = a.length >= size ? a :
                      (T[])java.lang.reflect.Array
                      .newInstance(a.getClass().getComponentType(), size);
            Iterator<E> it = iterator();
    
            for (int i = 0; i < r.length; i++) {
                if (! it.hasNext()) { // fewer elements than expected
                    if (a == r) {
                        r[i] = null; // null-terminate
                    } else if (a.length < i) {
                        return Arrays.copyOf(r, i);
                    } else {
                        System.arraycopy(r, 0, a, 0, i);
                        if (a.length > i) {
                            a[i] = null;
                        }
                    }
                    return a;
                }
                r[i] = (T)it.next();
            }
            // more elements than expected
            return it.hasNext() ? finishToArray(r, it) : r;
        }
  • 相关阅读:
    怎么快速掌握一门新技术
    Linq相关
    C# 参数按照ASCII码从小到大排序(字典序)
    测试工具
    sql 创建临时表
    sql行合并
    WCF相关
    免费开源分布式系统日志收集框架 Exceptionless
    VPS,虚拟主机,云主机,独立服务器区别
    c# Dictionary的遍历和排序
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/7047511.html
Copyright © 2011-2022 走看看