ArrayList 源码详解

ArrayList 继承 AbstractList 抽象类 是List 的子类
在这里插入图片描述在这里插入图片描述

属性解释

// 默认 数组大小是 10个
private static final int DEFAULT_CAPACITY = 10;
private static final Object[] EMPTY_ELEMENTDATA = {};
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
//存放数据域
transient Object[] elementData;
//当前有多少个元素
private int size;

构造方法解释

// 有参构造 传入自定义ArrayList大小 
	public ArrayList(int initialCapacity) {
		// 大于0 
        if (initialCapacity > 0) {
            // 直接 new 一个 initialCapacity 大小的 Object[]  不触发扩容机制 容量为initialCapacity
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
        	// 如果为0 就直接等于默认的空数组
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
        	// 小于0 就抛出异常
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }
// 无参构造 默认new 的方式
	public ArrayList() {
		//  数据数组 就直接等于默认的空数组 size=0 默认容量为 10
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }
// 有参构造 传入一个包含collection的ArrayList C
	public ArrayList(Collection<? extends E> c) {
        elementData = c.toArray(); // 转换为 Array
        if ((size = elementData.length) != 0) {
        	// size 赋值为 elementData的长度 如果不等于0 
        	
            if (elementData.getClass() != Object[].class)
                elementData = Arrays.copyOf(elementData, size, Object[].class);//复制指定数组,使elementData具有指定长度
        } else {
            // 没有元素
            this.elementData = EMPTY_ELEMENTDATA;
        }
    }

add方法解释

	// 传入一个 变量加入数组末尾
    public boolean add(E e) {
        modCount++;
       // 套娃调用 
        add(e, elementData, size);
        return true;
    }
    // 将元素 存放到指定位置
	public void add(int index, E element) {
        rangeCheckForAdd(index); //判断index 合法性  index <= size && index >= 0
        modCount++;
        final int s;
        Object[] elementData;
        // 如果 数组存放个数 等于数组最大长度 进行扩容 
        if ((s = size) == (elementData = this.elementData).length)
            elementData = grow(); // 扩容
        //数组elementData从index位置开始,复制到index+1位置,共复制size-index个元素
        System.arraycopy(elementData, index,
                         elementData, index + 1,
                         s - index);
        elementData[index] = element;
        size = s + 1;
    }
    // 调用 增加一个元素 如果数组满了 进行扩容
	private void add(E e, Object[] elementData, int s) {
        if (s == elementData.length)
            elementData = grow();
        elementData[s] = e;
        size = s + 1;
    }
    //在指定位置开始 增加一个collection的ArrayList C
    public boolean addAll(int index, Collection<? extends E> c) {
        rangeCheckForAdd(index);

        Object[] a = c.toArray();
        modCount++;
        int numNew = a.length;
        if (numNew == 0)
            return false;
        Object[] elementData;
        final int s;
        // 如果不能插入这么多元素 进行扩容
        if (numNew > (elementData = this.elementData).length - (s = size))
            elementData = grow(s + numNew);

        int numMoved = s - index;
        //拷贝
        if (numMoved > 0)
            System.arraycopy(elementData, index,
                             elementData, index + numNew,
                             numMoved);
        System.arraycopy(a, 0, elementData, index, numNew);
        size = s + numNew;
        return true;
    }
    // 增加一个Collection 的Arraylist c
 	public boolean addAll(Collection<? extends E> c) {
        Object[] a = c.toArray();
        modCount++;
        int numNew = a.length;
        if (numNew == 0)
            return false;
        Object[] elementData;
        final int s;
        if (numNew > (elementData = this.elementData).length - (s = size))
            elementData = grow(s + numNew);
        System.arraycopy(a, 0, elementData, s, numNew);
        size = s + numNew;
        return true;
    }

remove 方法解释

//	删除指定位置的元素
    public E remove(int index) {
        Objects.checkIndex(index, size);
        final Object[] es = elementData;

        @SuppressWarnings("unchecked") E oldValue = (E) es[index];
        fastRemove(es, index); // 移除

        return oldValue;
    }
    //实现移除
    private void fastRemove(Object[] es, int i) {
        modCount++;
        final int newSize;
        if ((newSize = size - 1) > i)
            System.arraycopy(es, i + 1, es, i, newSize - i);
        es[size = newSize] = null;
    }
   // 移除 ArrayList 中首次出现的指定元素对象 o 如果存在的话
  public boolean remove(Object o) {
        final Object[] es = elementData;
        final int size = this.size;
        int i = 0;
        found: {
            if (o == null) {
                for (; i < size; i++)
                    if (es[i] == null)
                        break found;
            } else {
                for (; i < size; i++)
                    if (o.equals(es[i]))
                        break found;
            }
            return false;
        }
        fastRemove(es, i);
        return true;
    }
    //移除ArrayList中包含在c中的元素
   public boolean removeAll(Collection<?> c) {
        return batchRemove(c, false, 0, size);
    }
    

扩容方法

	// 套娃
	private Object[] grow() {
        return grow(size + 1);
    }
    // 返回了一个扩容后的
    private Object[] grow(int minCapacity) {
        return elementData = Arrays.copyOf(elementData, newCapacity(minCapacity));
    }
    // 获得新的大小容量 
   private int newCapacity(int minCapacity) {
        // 赋值长度
        int oldCapacity = elementData.length;
        // 新的容量 等于 旧的容量 加上 旧的/2  
        // 例如 原来容量等于10  新的容量 就等于 10+ 10/2 = 15 
        // 扩容了1.5倍
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        // 如果新的容量 比 最小的容量还小
        if (newCapacity - minCapacity <= 0) {
        	// 如果 数据数组 为 默认的空的
            if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
            // 返会 最小容量和默认容量(10) 大的数
                return Math.max(DEFAULT_CAPACITY, minCapacity);
             //最小容量小于0 抛出异常
            if (minCapacity < 0) // overflow
                throw new OutOfMemoryError();
            //返回 最小容量 
            return minCapacity;
        }
        // 如果新的容量 小于 最大Array 长度( Integer.MAX_VALUE - 8;) 就返回 新的容量
       // 否者就调用hugeCapacity
        return (newCapacity - MAX_ARRAY_SIZE <= 0)
            ? newCapacity
            : hugeCapacity(minCapacity);
    }
    // 静态方法 
     private static int hugeCapacity(int minCapacity) {
        //如果最小容量 小于0  抛出异常
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
         // 如果最小容量大于 Array最大长度 就返回 Integer最大值 否者就是最大Array 长度  Integer.MAX_VALUE - 8;
        return (minCapacity > MAX_ARRAY_SIZE)
            ? Integer.MAX_VALUE
            : MAX_ARRAY_SIZE;
    }

迭代器方法解释

	// 返回一个迭代器对象 Itr 内部类 实现了Iterator接口
	 public Iterator<E> iterator() {
        return new Itr();
    }
         */
    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;
	.....
	}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值