java基础-集合-ArrayList(JDK1.8)源码学习

文章目录

  • 类图
  • 新增
    • add
    • ensureCapacityInternal
    • ensureExplicitCapacity
    • grow
    • hugeCapacity
  • 删除
    • remove
    • fastRemove
  • 遍历
    • Iterator

类图

java基础-集合-ArrayList(JDK1.8)源码学习_第1张图片

新增

add

    public boolean add(E e) {
    	// 根据注释可知 Increments modCount!!,modCount下面详解
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        // elementData数组最后一个位置存放新元素
        elementData[size++] = e;
        return true;
    }

ensureCapacityInternal

private void ensureCapacityInternal(int minCapacity) {
		// elementData 未初始化,设置最小容量为DEFAULT_CAPACITY:10
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }

        ensureExplicitCapacity(minCapacity);
    }

ensureExplicitCapacity

    private void ensureExplicitCapacity(int minCapacity) {
        modCount++;

        // elementData小于需要的最小容量,扩容
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }

grow

private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        // 扩容旧容量的一半
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        // 如果仍然不够,则取需要的容量大小
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        // 最小容量大于Integer.MAX_VALUE - 8;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

hugeCapacity

    private static int hugeCapacity(int minCapacity) {
    	// 初看这个判断时一直没明白什么情况下会传如一个小于0的值,
    	// 原来是在上一个方法的 int newCapacity = oldCapacity + (oldCapacity >> 1);代码中
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }

删除

remove

    public boolean remove(Object o) {
        if (o == null) {
            for (int index = 0; index < size; index++)
                if (elementData[index] == null) {
                    fastRemove(index);
                    return true;
                }
        } else {
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }

fastRemove

    private void fastRemove(int index) {
        modCount++;
        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
    }

遍历

Iterator

private class Itr implements Iterator<E> {
        int cursor;       // index of next element to return
        int lastRet = -1; // index of last element returned; -1 if no such
        int expectedModCount = modCount;

        public boolean hasNext() {
            return cursor != size;
        }

        @SuppressWarnings("unchecked")
        public E next() {
            checkForComodification();
            int i = cursor;
            if (i >= size)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i + 1;
            return (E) elementData[lastRet = i];
        }

       ......

        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }
  • ArrayList内部类Itr 实现了接口Iterator,重写方法hasNextnext
  • 方法checkForComodification()判断上文中提到的modCount 是已被修改,若修改则出错;
  • 删除方法fastRemove中会modCount++
  • 新增方法ensureExplicitCapacity中会modCount

你可能感兴趣的:(java,学习,开发语言)