1. Java implementation of table
We programmers introduced the C language at the beginning
1.1 Array implementation
The main reason is that the query is fast, deletion, and insertion time complexity is O(N). For example, if the first element is deleted, then the following elements will move forward as a whole, and the query is relatively simple. The time complexity is O(1)
1.2 Linked list implementation: fast insertion and deletion, complex query
2. ArrayList array implementation
Predefined Basic Properties
// default capacity private static final int DEFAULT_CAPACITY = 10; // length private int size; // element private E [] items;Use an array to store the elements in the list.
2.1 capacity
What is the concept of capacity: It feels very intuitive to understand that it is a pre-estimated capacity, which provides a preparation for the new elements that come later. It is not necessary to do expansion processing every time a new element is added, which affects the efficiency (so ArrayList is in space This part above is wasteful).
public void ensureCapacity(int newCapacity){ if ( newCapacity < size) { return; } E [] old = items; items = (E []) new Objects [newCapacity]; for (int i = 0; i < size(); i++) { items [i] = old[i]; } }
2.2 add
public void add (And he) { add (size (), ele); } public void add (int index, E ele) { if ( items.length == size()) { // Capacity expansion mechanism jdk 1.5 times + 1 ensureCapacity (size() * 2 + 1); } // Insert point, move backward, with position behind for ( int i = size; i > index; i-- ) { items [i] = items [i - 1]; } items[index] = ele; }
2.3 remove
public E remove (int index) { E removeItem = items[index]; // indent forward for ( int i = index; i < size() - 1; i++) { items [i] = items[i - 1]; } size --; return removeItem; }
3. LinkedList implementation
3.1 Node node
LinkedList is implemented by a double-linked list and is composed of nodes, so we also define a node class Node (internal nesting)
public class Node<E> { public E data; public Node<E> prev; public Node<E> next; public Node(E data, Node<E> prev, Node<E> next){ this.data = data; this.prev = prev; this.next = next; } }
initialization
public MyLinkedList () { clear(); } public void clear () { beginMarker = new Node<E>(null, null, null); endMarker= new Node<E>(null, beginMarker, null); beginMarker.next = endMarker; size = 0; }
3.2 Node insertion point, delete point
Due to the addition and deletion of the linked list, it is related to the node at the operation position, so the operation node at the position should be obtained before the operation
public Node<E> getNode (int index){ return getNode(index, 0, size()-1); } public Node<E> getNode (int index, int lower, int upper){ Node<E> p; if(index < lower || index > upper) { throw new IndexOutOfBoundsException(); } // Determine whether to start from the front or from the back, and reduce the number of loops if (index < size()/2) { p = beginMarker; for (int i = 0; i < index; i++){ p = p.next; } }else { p = endMarker; for (int i = size(); i > index; i--) { p = p.prev; } } return p; }
3.3 Node new
public void add(int index, E ele){ addBefore( getNode(index, 0, size()), ele); } public void addBefore(Node<E> p, E e){ Node<E> newNode = new Node<E>(e, p.prev, p); p.prev.next = newNode; p.prev = newNode; size ++; }
3.3 Node deletion
public void romove(int index){ romove(getNode(index, 0, size()-1)); } public void romove(Node<E> p){ p.prev.next = p.next; p.next.prev = p.prev; size--; }
4. Summary
ArrayList, LinkedList are asynchronous. This is different from Vector