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


    类图

    在这里插入图片描述

    新增

    add

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

    ensureCapacityInternal

    private void ensureCapacityInternal(int minCapacity) {
    		// elementData 未初始化,设置最小容量为DEFAULT_CAPACITY:10
            if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
                minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
            }
    
            ensureExplicitCapacity(minCapacity);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    ensureExplicitCapacity

        private void ensureExplicitCapacity(int minCapacity) {
            modCount++;
    
            // elementData小于需要的最小容量,扩容
            if (minCapacity - elementData.length > 0)
                grow(minCapacity);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    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);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    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;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    删除

    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;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    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
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    遍历

    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();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • ArrayList内部类Itr 实现了接口Iterator,重写方法hasNextnext
    • 方法checkForComodification()判断上文中提到的modCount 是已被修改,若修改则出错;
    • 删除方法fastRemove中会modCount++
    • 新增方法ensureExplicitCapacity中会modCount
  • 相关阅读:
    【译】自制前端玩具框架
    RabbitMQ介绍篇(一)
    “undefined reference to XXX“问题总结
    iApp祁天社区UI成品源码 功能齐全的社区应用
    Spring中的IOC控制Mybatis案例
    小说推文是一种通过短视频平台推广小说的方式
    ospf多区域原理和配置
    InnoDB 事务
    什么情况下使用微服务?
    动静态库--Linux
  • 原文地址:https://blog.csdn.net/Semanteme/article/details/132920157