常见集合类(ArrayList)

ArrayList

  • 主要成员变量:
transient Object[] elementData; //底层是一个数组结构,用来保存元素
private static final int DEFAULT_CAPACITY = 10;//默认初始化容量大小为10

构造方法

在这里插入图片描述
主要构造方法: public ArrayList(int initialCapacity)

public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            //创建一个指定大小的Object数组
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }

add方法

add(E e)&add(int index, E element)两个重载

在这里插入图片描述

public boolean add(E e) {
        //1. 确定list的空间大小
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        //2. 在数组从左向右第一个为null的位置填充e
        elementData[size++] = e;
        return true;
    }

ensureCapacityInternal(int minCapacity)方法:相当于grow方法的一个前奏吧

private void ensureCapacityInternal(int minCapacity) {
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            //最小的容量为10
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }
        ensureExplicitCapacity(minCapacity);
    }
    
private void ensureExplicitCapacity(int minCapacity) {
        modCount++;
        // overflow-conscious code
        //如果最大容量已经达到,对数组进行扩容
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }

grow方法:就是用来对底层的数组结构进行扩容

 private void grow(int minCapacity) {
        // 旧的数组长度
        int oldCapacity = elementData.length;
        //新的数组长度 = 旧的数组长度 + 旧的数组长度*0.5
        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:
        //把原来的数组里面的元素复制到新数组里面,并且把当前list的元素指针指向新的数组
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

get方法

这个方法就是返回执行下标位置的元素

public E get(int index) {
	    //检查数组下标是否越界
        rangeCheck(index);       
        return elementData(index);
    }

remove方法

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;
    }

    /**
     * Removes the first occurrence of the specified element from this list,
     * if it is present.  If the list does not contain the element, it is
     * unchanged.  More formally, removes the element with the lowest index
     * <tt>i</tt> such that
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
     * (if such an element exists).  Returns <tt>true</tt> if this list
     * contained the specified element (or equivalently, if this list
     * changed as a result of the call).
     *
     * @param o element to be removed from this list, if present
     * @return <tt>true</tt> if this list contained the specified element
     */
    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;
    }

    /*
     * Private remove method that skips bounds checking and does not
     * return the value removed.
     */
    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
    }

public static native void arraycopy(Object src, int srcPos,Object dest, int destPos,
int length);
😒

这个方法主要就是完成数组的复制

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值