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

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

杨魅力
浏览 650回答 1
1回答

达令说

看源码:/**&nbsp; &nbsp; &nbsp;* The array buffer into which the elements of the ArrayList are stored.&nbsp; &nbsp; &nbsp;* The capacity of the ArrayList is the length of this array buffer. Any&nbsp; &nbsp; &nbsp;* empty ArrayList with elementData == EMPTY_ELEMENTDATA will be expanded to&nbsp; &nbsp; &nbsp;* DEFAULT_CAPACITY when the first element is added.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private transient Object[] elementData;&nbsp;/**&nbsp; &nbsp; &nbsp;* Constructs a list containing the elements of the specified&nbsp; &nbsp; &nbsp;* collection, in the order they are returned by the collection's&nbsp; &nbsp; &nbsp;* iterator.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param c the collection whose elements are to be placed into this list&nbsp; &nbsp; &nbsp;* @throws NullPointerException if the specified collection is null&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public ArrayList(Collection<? extends E> c) {&nbsp; &nbsp; &nbsp; &nbsp; elementData = c.toArray();&nbsp; &nbsp; &nbsp; &nbsp; size = elementData.length;&nbsp; &nbsp; &nbsp; &nbsp; // c.toArray might (incorrectly) not return Object[] (see 6260652)&nbsp; &nbsp; &nbsp; &nbsp; if (elementData.getClass() != Object[].class)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elementData = Arrays.copyOf(elementData, size, Object[].class);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java