ArrayList Source Code

ArrayList

  • public E remove(int index)
    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; // clear to let GC do its work

        return oldValue;
    }

remove 移除index位置的元素: 首先计算index后面有多少个元素(numMoved),如果元素个数多于1个,使用arrayCopy将index后面所有元素整体向前移动一位