了解LinkedList原理
public LinkedList() { header.next = header.previous = header; // 先初始化一个空的Entry,用来做header,然后首尾相连,形成一个循环链表 } public boolean remove(Object o) { if (o==null) { //分为空和非空删除 for (Entry<E> e = header.next; e != header; e = e.next) { if (e.element==null) { remove(e); return true; } } } else { for (Entry<E> e = header.next; e != header; e = e.next) { if (o.equals(e.element)) { remove(e); return true; } } } return false; }//查找private Entry<E> entry(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+size); Entry<E> e = header; if (index < (size >> 1)) {//size >> 1 相当于 size/2 通过index确定是向前还是向后查找 for (int i = 0; i <= index; i++) e = e.next; } else { for (int i = size; i > index; i--) e = e.previous; } return e; }final void checkForComodification() { //检测是否在并发 if (modCount != expectedModCount)throw new ConcurrentModificationException();} private Entry<E> addBefore(E e, Entry<E> entry) { //因为是循环链表,插入到最前面也就是添加到最后Entry<E> newEntry = new Entry<E>(e, entry, entry.previous);newEntry.previous.next = newEntry;newEntry.next.previous = newEntry;size++;modCount++;return newEntry; }
?
?
4.优化
?LinkedList 应用在新增和删除操作比较频繁场景
?
?
?
?
?
?
?
?
?
?
?