【预习笔记】读几个Java集合类源代码—ArrayList

来源:互联网 发布:待业在家知乎 编辑:程序博客网 时间:2024/05/23 21:56

这系笔记记录了对Java中几个常用集合类的源代码实现的学习

包括ArrayList,LinkedList,HashMap,HashTable, ConcurrentHashMap。


ArrayList

public class ArrayList<E> extends AbstractList<E>        implements List<E>, RandomAccess, Cloneable, java.io.Serializable{    private static final long serialVersionUID = 8683452581122892189L;    /**     * The array buffer into which the elements of the ArrayList are stored.     * The capacity of the ArrayList is the length of this array buffer.     */    private transient Object[] elementData;    /**     * The size of the ArrayList (the number of elements it contains).     *     * @serial     */    private int size;

从声明以及几个成员变量的定义来看,首先ArrayList是可序列化的,其内部定义了一个类型为Object的一维数组,以及一维数组的大小size。

    public ArrayList(int initialCapacity) {super();        if (initialCapacity < 0)            throw new IllegalArgumentException("Illegal Capacity: "+                                               initialCapacity);this.elementData = new Object[initialCapacity];    }    /**     * Constructs an empty list with an initial capacity of ten.     */    public ArrayList() {this(10);    }    /**     * Constructs a list containing the elements of the specified     * collection, in the order they are returned by the collection's     * iterator.     *     * @param c the collection whose elements are to be placed into this list     * @throws NullPointerException if the specified collection is null     */    public ArrayList(Collection<? extends E> c) {elementData = c.toArray();size = elementData.length;// c.toArray might (incorrectly) not return Object[] (see 6260652)if (elementData.getClass() != Object[].class)    elementData = Arrays.copyOf(elementData, size, Object[].class);    }

构造方法上看,如果不指定长度的大小,则默认数组长度为10。

如果以某个继承了Collection的类作为构造参数传入,则会把传进来的数组赋值为成员变量数组。

public boolean contains(Object o) {return indexOf(o) >= 0;    }    /**     * Returns the index of the first occurrence of the specified element     * in this list, or -1 if this list does not contain the element.     * More formally, returns the lowest index <tt>i</tt> such that     * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,     * or -1 if there is no such index.     */    public int indexOf(Object o) {if (o == null) {    for (int i = 0; i < size; i++)if (elementData[i]==null)    return i;} else {    for (int i = 0; i < size; i++)if (o.equals(elementData[i]))    return i;}return -1;    }

在调用contains的时候,由于是线性存储结构,所以实际上需要遍历整个数组,所以调用contains的复杂度实际上为O(n),如果在嵌套循环中使用contains,则会造成O(n^2)的复杂度。刷题的时候需要注意....

public boolean add(E e) {ensureCapacity(size + 1);  // Increments modCount!!elementData[size++] = e;return true;    }    /**     * Inserts the specified element at the specified position in this     * list. Shifts the element currently at that position (if any) and     * any subsequent elements to the right (adds one to their indices).     *     * @param index index at which the specified element is to be inserted     * @param element element to be inserted     * @throws IndexOutOfBoundsException {@inheritDoc}     */    public void add(int index, E element) {if (index > size || index < 0)    throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);ensureCapacity(size+1);  // Increments modCount!!System.arraycopy(elementData, index, elementData, index + 1, size - index);elementData[index] = element;size++;    }
add方法就是放数组的末尾位置添加一个元素,不过首先需要确保数组添加元素之后不会越界。

调用ensureCapacity实现。

像某个指定位置插入元素首先把index到size - 1的元素依次后移一位,然后把index位置的元素置为element。

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

可以看到ensureCapacity的实现:如果越界则扩容1.5倍。如默认初始长度第一次扩容变为16,再扩容变为25....

扩容之后还需要进行一次复制操作。可以注意到这里的复制调用的是Arrays的方法,而往指定位置插入元素的时候调用的是System的native方法,两者应该是存在效率上的区别。

原创粉丝点击