首页 > 试题广场 >

ArrayList list = new&n...

[单选题]
ArrayList list = new ArrayList(20);中的list扩充几次
  • 0
  • 1
  • 2
  • 3
T_*头像 T_*
ArrayList list=new ArrayList(); 这种是默认创建大小为10的数组,每次扩容大小为1.5倍 ArrayList list=new ArrayList(20); 这种是指定数组大小的创建,创建时直接分配其大小,没有扩充。 所以,扩充为0次
发表于 2019-09-04 08:03:03 回复(0)

默认长度是10,然后自己指定了20,所以没有扩容?

发表于 2019-08-21 23:33:41 回复(2)
    /**
     * 传入初始容量初始化
     * Constructs an empty list with the specified initial capacity.
     *
     * @param  initialCapacity  the initial capacity of the list
     * @throws IllegalArgumentException if the specified initial capacity
     *         is negative
     */
    public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            // 初始化容量大于 0 的话,就重新 new 一个数组
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            // 初始化容量等于 0 的话,初始化为空数组 EMPTY_ELEMENTDATA
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            // 初始化容量小于 0 的话就抛出异常
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }

发表于 2019-10-12 15:02:42 回复(0)
指定初始化长度为20不需要扩容
发表于 2019-08-22 10:24:24 回复(0)

ArrayList初始长度为20,所以没有扩容!

发表于 2019-08-20 22:42:58 回复(2)