ArrayList前世今生

1、 ArrayList默认的初始化因子大小是10。

/**
     * Constructs an empty list with an initial capacity of ten.//一般初始化因子为10
     */
    public ArrayList() {
    this(10);// 调用2中的ArrayList(int initialCapacity);
    }


2、用来生成一个大小为initialCapacity的ArrayList。其底层实现为一Object数组。

/**
     * Constructs an empty list with the specified initial capacity.//用一个初始容量来构建一个空的list
     *
     * @param   initialCapacity   the initial capacity of the list.
     * @exception IllegalArgumentException if the specified initial capacity
     *            is negative
     */
    public ArrayList(int initialCapacity) {
    super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
    this.elementData = (E[])new Object[initialCapacity];
    }

 

3、添加一个元素到列表的末尾。

 

 /**
     * Appends the specified element to the end of this list.
     *
     * @param o element to be appended to this list.
     * @return <tt>true</tt> (as per the general contract of Collection.add).
     */
    public boolean add(E o) {
    ensureCapacity(size + 1);  // Increments modCount!!
    elementData[size++] = o;
    return true;
    }

 

4、当添加一个元素时,如果列表已满,这是就要添加列表容量。新的容量为(原容量* 3)/2 + 1。

 

/**
     * Increases the capacity of this <tt>ArrayList</tt> instance, if
     * necessary, to ensure  that it can hold at least the number of elements
     * specified by the minimum capacity argument.
     *
     * @param   minCapacity   the desired minimum capacity.
     */
    public void ensureCapacity(int minCapacity) {
    modCount++;
    int oldCapacity = elementData.length;
    if (minCapacity > oldCapacity) {
        Object oldData[] = elementData;
        int newCapacity = (oldCapacity * 3)/2 + 1;
            if (newCapacity < minCapacity)
        newCapacity = minCapacity;
        elementData = (E[])new Object[newCapacity];
        System.arraycopy(oldData, 0, elementData, 0, size);
    }
    }


5、删除列表中指定位置的元素,将后面的元素前移。

 

/**
     * Removes the element at the specified position in this list.
     * Shifts any subsequent elements to the left (subtracts one from their
     * indices).
     *
     * @param index the index of the element to removed.
     * @return the element that was removed from the list.
     * @throws    IndexOutOfBoundsException if index out of range <tt>(index
     *           &lt; 0 || index &gt;= size())</tt>.
     */
    public E remove(int index) {
    RangeCheck(index);

    modCount++;
    E oldValue = elementData[index];

    int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index,
                 numMoved);
    elementData[--size] = null; // Let gc do its work

    return oldValue;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值