ArrayList源码

构造方法

	/**
     * Constructs an empty list with an initial capacity of ten.
     */
	public ArrayList() {
	     this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
	}


	//创建指定长度
	//大于0创建一个对应长度的Object数组
	//等于0就创建一个默认的空数组
	//否则抛出一个异常
	 public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }
    

	//
	public ArrayList(Collection<? extends E> c) {
        elementData = c.toArray();
        if ((size = elementData.length) != 0) {
            // c.toArray might (incorrectly) not return Object[] (see 6260652)
            if (elementData.getClass() != Object[].class)
                elementData = Arrays.copyOf(elementData, size, Object[].class);
        } else {
            // replace with empty array.
            this.elementData = EMPTY_ELEMENTDATA;
        }
    }



添加方法


	public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }

	//扩容入口
	private void ensureCapacityInternal(int minCapacity) {
        ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
    }

	//计算最小容量
	private static int calculateCapacity(Object[] elementData, int minCapacity) {
		//如果数组为空,返回默认容量和最小容量中的较大值
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
        	//默认容量10
            return Math.max(DEFAULT_CAPACITY, minCapacity);
        }
        return minCapacity;
    }

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

        // overflow-conscious code
        //最小容量如果大于了当前数组的容量
        if (minCapacity - elementData.length > 0)
        //扩容
            grow(minCapacity);
    }

	
	private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        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);
    }
    

删除

	public E remove(int index) {
		//判断是否越界
        rangeCheck(index);

        modCount++;
        //得到要删除的元素
        E oldValue = elementData(index);

		//计算出需要移动覆盖的长度
        int numMoved = size - index - 1;
        //拷贝数组,相当于将后面的数组元素全部往前移动1个位置
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work

        return oldValue;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值