为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

杨魅力 2019-03-12 18:19:44
今天看到Arrays.asList()方法中使用的是一个私有的ArrayList使用了参数为数组的构造器,为什么ArrayList本身不带有这样的构造器呢?
查看完整描述

1 回答

?
达令说

TA贡献1821条经验 获得超6个赞

看源码:


/**

     * 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 collection's

     * iterator.

     *

     * @param c the collection whose elements are to be placed into this list

     * @throws NullPointerException if the specified collection is null

     */

    public ArrayList(Collection<? 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.copyOf(elementData, size, Object[].class);

    }


查看完整回答
反对 回复 2019-04-25
  • 1 回答
  • 0 关注
  • 649 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信