java中的ArrayList为什么没有以数组为参数的构造器?
澳门新葡京
澳门新葡京
当前位置 : 澳门新葡京 > 澳门葡京赌场官网

java中的ArrayList为什么没有以数组为参数的构造器?

今天看到Arrays.asList方法中使用的是一个私有的ArrayList使用了参数为数组的构造器,为什么ArrayList本身不带有这样的构造器呢?

看源码:

/**
     * The array buffer into which the elements of the ArrayList are stored.
     * The capacity of the ArrayList is the length of this array buffer. Any
     * empty ArrayList with elementData == EMPTY_ELEMENTDATA will be expanded to
     * DEFAULT_CAPACITY when the first element is added.
     */
    private transient Object[] elementData;
 /**
     * Constructs a list containing the elements of the specified
     * collection, in the order they are returned by the collections
     * iterator.
     *
     * @param c the collection whose elements are to be placed into this list
     * @throws NullPointerException if the specified collection is null
     */
    public ArrayListCollection<? extends E> c {
        elementData = c.toArray;
        size = elementData.length;
        // c.toArray might incorrectly not return Object[] see 6260652
        if elementData.getClass != Object[].class
            elementData = Arrays.copyOfelementData, size, Object[].class;
    }

广告位

澳门新葡京