ArrayList source code相关方法阅读

来源:互联网 发布:mac苹果手机铃声设置 编辑:程序博客网 时间:2024/06/03 20:59

1、新增一个对象

/**     * Appends the specified element to the end of this list.     *     * @param e element to be appended to this list     * @return <tt>true</tt> (as specified by {@link Collection#add})     */    public boolean add(E e) {ensureCapacity(size + 1);  // Increments modCount!!elementData[size++] = e;return true;    }
 /**     * Increases the capacity of this <tt>ArrayList</tt> instance, if     * necessary, to ensure that it can hold at least the number of elements     * specified by the minimum capacity argument.     *     * @param   minCapacity   the desired minimum capacity     */    public void ensureCapacity(int minCapacity) {modCount++;int oldCapacity = elementData.length;if (minCapacity > oldCapacity) {    Object oldData[] = elementData;    int newCapacity = (oldCapacity * 3)/2 + 1;        if (newCapacity < minCapacity)newCapacity = minCapacity;            // minCapacity is usually close to size, so this is a win:            elementData = Arrays.copyOf(elementData, newCapacity);}    }

源码里面elementData对象是这样定义:private transient Object[] elementData;

在之前的开发中从来没用过transient 关键字,特意测试了一下,发现transient就是persistent的对立,当用到字节流、网络传输的时候一旦属性定义为transient将会出现获取值为null的情况。

详情可参考:What is transient keyword in Java?

还有一个需要了解知识点的地方就是Arrays.copyOf(elementData, newCapacity)方法,它是将elementData数组的数据按照newCapacity长度建立一个新的数组的同时,将原数组elementData数组里面的数据拷贝到新的数组中。

2、移除一个对象

/**     * 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 ? get(i)==null : 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; // Let gc do its work    }

这里面关键点就是fastRemove方法中的System.arraycopy(elementData, index+1, elementData, index, numMoved);

这个方法的含义是将elementData数组从index+1位置到numMoved位置的数据拷贝到数组elementData的index位置,依次往后填充

详情可参考:老紫竹

以上两点我们可以看出:list其实的存储其实就是数组;数组都是按位来存储,所以在插入或者是移除的时候都是依次顺序往后推移,所以我们的面试题经常会提到ArrayList查询效率要高于新增和删除的效率

这里暂时只列出两个方法,其他今后有时间研究再这里列出来,待续....

0 0
原创粉丝点击