ArrayList & LinkedList

结构图:

ArrayList适合于查询较多的场景,因为查询的时间复杂度是o(1);LinkedList适合于修改插入、删除比较多的场景,因为插入、删除的时间复杂度为o(1)。

ArrayList是基于数组实现的List类,它封装了一个动态的增长的、允许再分配的Object[]数组。

主要结构:

/**
 * 动态数组
 */
private transient Object[] elementData;

/**
 * 数组大小
 */
private int size;

 

添加元素:

public boolean add(E e) {
	// 看是否需要扩容
    ensureCapacityInternal(size + 1); 
    elementData[size++] = e;
    return true;
}
 
private void ensureCapacityInternal(int minCapacity) {
    if (elementData == EMPTY_ELEMENTDATA) {
        minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
    }

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

    // 空间不够,进行扩容
    if (minCapacity - elementData.length > 0)
        grow(minCapacity);
}
 
private void grow(int minCapacity) {
    int oldCapacity = elementData.length;
	//扩容为原来数组大小的1.5倍
    int newCapacity = oldCapacity + (oldCapacity >> 1);
    if (newCapacity - minCapacity < 0)
        newCapacity = minCapacity;
    if (newCapacity - MAX_ARRAY_SIZE > 0)
        newCapacity = hugeCapacity(minCapacity);
    elementData = Arrays.copyOf(elementData, newCapacity);
}

  

删除元素:

//删除固定位置的元素
public E remove(int index) {
    rangeCheck(index);

    modCount++;
    E oldValue = elementData(index);

    int numMoved = size - index - 1;
	//整个数组前移1位,并把最后的元素置为null,让gc回收
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
    elementData[--size] = null; 

    return oldValue;
}

  

LinkedList是基于链表实现的List类。

主要结构:

/**
 *   链表大小
 */
transient int size = 0;

/**
 * 指向头节点的指针
 */
transient Node<E> first;

/**
 * 指向尾节点的指针
 */
transient Node<E> last;

  

Node结构:

private static class Node<E> {
    E item;
    /**
 	* 指向下一个元素的指针
 	*/
    Node<E> next;
	/**
 	* 指向前一个元素的指针
 	*/
    Node<E> prev;

    Node(Node<E> prev, E element, Node<E> next) {
        this.item = element;
        this.next = next;
        this.prev = prev;
    }
}

  

添加元素:

public boolean add(E e) {
	//插入到链表最后
    linkLast(e);
    return true;
}
 
void linkLast(E e) {
    final Node<E> l = last;
    final Node<E> newNode = new Node<>(l, e, null);
    last = newNode;
    if (l == null)
        first = newNode;
    else
        l.next = newNode;
    size++;
    modCount++;
}

  

删除元素:

public boolean remove(Object o) {
    if (o == null) {
        for (Node<E> x = first; x != null; x = x.next) {
            if (x.item == null) {
                unlink(x);
                return true;
            }
        }
    } else {
        for (Node<E> x = first; x != null; x = x.next) {
            if (o.equals(x.item)) {
                unlink(x);
                return true;
            }
        }
    }
    return false;
}
 
E unlink(Node<E> x) {
    // assert x != null;
    final E element = x.item;
    final Node<E> next = x.next;
    final Node<E> prev = x.prev;

    if (prev == null) {
        first = next;
    } else {
        prev.next = next;
        x.prev = null;
    }

    if (next == null) {
        last = prev;
    } else {
        next.prev = prev;
        x.next = null;
    }

    x.item = null;
    size--;
    modCount++;
    return element;
}

  

获取元素:

public E get(int index) {
    checkElementIndex(index);
    return node(index).item;
}
 
Node<E> node(int index) {
    if (index < (size >> 1)) {
        Node<E> x = first;
        for (int i = 0; i < index; i++)
            x = x.next;
        return x;
    } else {
        Node<E> x = last;
        for (int i = size - 1; i > index; i--)
            x = x.prev;
        return x;
    }
}

  

 

猜你喜欢

转载自www.cnblogs.com/saiQsai/p/8994450.html
今日推荐