ArrayList: in essence, it is an array with dynamic expansion, which is 1.5 times of the original.
Core code: int newcapacity = oldcapacity + (oldcapacity > > 1); / / move one bit to the right to divide by 2
How to add or remove an element from ArrayList.
Add an element: judge if the array capacity is not enough, expand the capacity dynamically to 1.5 times of the original array capacity!!! And the data after the index of the added element is copied to the new array again.
Core code: System.arraycopy(elementData, index, elementData, index + 1,
size - index);
Delete an element: find the index of the deleted element, and copy the index of the deleted element to the new array.
Core code: System.arraycopy(elementData, index+1, elementData, index,
numMoved);
Concept introduction: copy process of ArrayList (source code is not implemented, I heard it is written in C or C + +).... From the Internet):
When copying again, if the basic data type is copied, the deep copy is used. The copied data type refers to the shallow copy
Deep copy: in the process of copying one object to another, a new object is created (a space is opened in memory and a new object address is owned), and the contents of the original object are copied to the new object again. When the content of the original object changes, the content of the new object will not be disturbed.
Shallow copy: in the process of copying one object to another, a new object is created. The new object points to the original object, and its object address does not change. When the content of the original object changes, the content of the new object will also change.
=============================================================================================
LinkedList: essentially a two-way linked list.
Node:
Add elements (find several diagrams and don't post them if you think they are misleading, just post the source code)
//Data structure of Node: private static class Node<E> { E item;//Element content Node<E> next;//Post pointer Node<E> prev;//Front pointer Node(Node<E> prev, E element, Node<E> next) { this.item = element; this.next = next; this.prev = prev; } } /** * Links e as first element. */ private void linkFirst(E e) { final Node<E> f = first;//Save the original first node //The precursor pointer of the newly added node must be empty, because the newly added node is the first node, and there is no node in front of it. final Node<E> newNode = new Node<>(null, e, f); first = newNode; if (f == null) last = newNode; else f.prev = newNode;//Place the newly added node in front of the original first node. size++; modCount++; } /** * Links e as last element. */ void linkLast(E e) { final Node<E> l = last;//Save the original last node //The backdrive pointer of the newly added node must be empty, because the newly added node is the last node, and there is no node after it. final Node<E> newNode = new Node<>(l, e, null); last = newNode; if (l == null) first = newNode; else l.next = newNode;//Put the newly added node after the last one. size++; modCount++; } //The process of adding an element is actually to add another element after the last node of the list public void add(E e) { checkForComodification(); lastReturned = null; if (next == null) linkLast(e); else linkBefore(e, next); nextIndex++; expectedModCount++; }
Remove element:
Remove an element: to remove an element, there will be a process of finding the element. When the current index position is less than 1 / 2 of the length of the linked list, scan from the first node to the back.
When the current index position is greater than 1 / 2 of the length of the linked list, scan from the tail node to the front.
/** * Removes the first occurrence of the specified element from this list, * if it is present. If this list does not contain the element, it is * unchanged. More formally, removes the element with the lowest index * {@code i} such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> * (if such an element exists). Returns {@code true} 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 {@code true} if this list contained the specified element */ 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; } /** * Unlinks non-null node x. */ 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; } /** * Returns the (non-null) Node at the specified element index. */ Node<E> node(int index) { // assert isElementIndex(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; } }