ArrayList & LinkedList

Structure diagram:

ArrayList is suitable for scenarios with many queries, because the time complexity of query is o(1); LinkedList is suitable for scenarios with many modification insertions and deletions, because the time complexity of insertion and deletion is o(1).

ArrayList is an array-based List class that encapsulates a dynamically growing Object[] array that allows reallocation.

The main structure:

/**
 * dynamic array
 */
private transient Object[] elementData;

/**
 * array size
 */
private int size;

 

Add elements:

public boolean add(E e) {
	// See if expansion is needed
    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++;

    // There is not enough space, expand the capacity
    if (minCapacity - elementData.length > 0)
        grow(minCapacity);
}
 
private void grow(int minCapacity) {
    int oldCapacity = elementData.length;
	//Expand to 1.5 times the size of the original array
    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);
}

  

Remove elements:

//delete the element at a fixed position
public E remove(int index) {
    rangeCheck (index);

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

    int numMoved = size - index - 1;
	//The entire array is moved forward by 1, and the last element is set to null, so that gc can recycle
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
    elementData[--size] = null;

    return oldValue;
}

  

LinkedList is a List class implemented based on linked list.

The main structure:

/**
 * List size
 */
transient int size = 0;

/**
 * pointer to the head node
 */
transient Node<E> first;

/**
 * pointer to the tail node
 */
transient Node<E> last;

  

Node structure:

private static class Node<E> {
    E item;
    /**
 	* pointer to the next element
 	*/
    Node<E> next;
	/**
 	* pointer to the previous element
 	*/
    Node<E> prev;

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

  

Add elements:

public boolean add(E e) {
	// insert at the end of the list
    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++;
}

  

Remove elements:

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

  

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

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325310673&siteId=291194637