JDK源码阅读——ArrayList(1)

作为Java集合框架下的一个重要的类,ArrayList类继承了AbstractList类,并扩展了List, RandomAccess, Cloneable, java.io.Serializable等接口。ArrayList的成员变量主要有三个数组:

private static final Object[] EMPTY_ELEMENTDATA = {};
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
transient Object[] elementData; // non-private to simplify nested class access

elementData肯定是用来存储元素的,那前两个static数组是做什么的呢?我们来看下ArrayList的构造方法:

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() {
    this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
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;
    }
}

ArrayList有三种构造方法,默认构造方法将DEFAULTCAPACITY_EMPTY_ELEMENTDATA数组赋值给elementData;int initialCapacity作为参数的构造方法创建一个大小为initialCapacity的数组,若initialCapacity == 0,则将EMPTY_ELEMENTDATA赋值给elementData;以Collection c作为参数的构造方法判断c.toArray的长度是否为0,是则将EMPTY_ELEMENTDATA赋值给elementData,否则根据情况将c.toArray赋给elementData,或者使用Arrays.copyOf创建一个新数组

get和set方法首先对传入的index参数进行检查是否越界,再对数组的index位置取值或赋值:

public E get(int index) {
    rangeCheck(index);
    return elementData(index);
}
public E set(int index, E element) {
    rangeCheck(index);
    E oldValue = elementData(index);
    elementData[index] = element;
    return oldValue;
}

add方法进行了重载,向数组末尾加入一个元素,或是向指定位置加入一个元素:

public boolean add(E e) {
    ensureCapacityInternal(size + 1);  // Increments modCount!!
    elementData[size++] = e;
    return true;
}
public void add(int index, E element) {
    rangeCheckForAdd(index);
    ensureCapacityInternal(size + 1);  // Increments modCount!!
    System.arraycopy(elementData, index, elementData, index + 1,
                     size - index);
    elementData[index] = element;
    size++;
}

其中System.arraycopy的原型是public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) ,将src从srcPos起始的length位复制到dest的destPos位之后的连续空间

remove方法同样进行了重载,以int index作为参数时,调用System.arraycopy将index之后的元素向前挪一位;以Object o为参数时,利用o.equals找到跟o相等的第一个元素,调用private方法fastRemove(int 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;
}
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 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
}

这里产生了一个疑惑:既然fastRemove的代码和remove(int index)的大部分代码相同,为何在remove中不直接调用fastRemove?是否是为了提高效率而减少方法的调用?那为何在第二种remove中不直接把代码展开?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值