public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable
LinkedList是一个双向链表的实现,允许所有的元素,包括null。
对于index操作,会从链头到链尾地搜索,即使它靠近一个特殊索引位置。
非线程安全的,在多线程环境下,需要外部同步或调用Collections.synchronizedList(new LinkedList(...));获得一个线程安全的链表。
迭代器是快速失败的。
3个实例变量
//链表大小 transient int size = 0; //链头 transient Node<E> first; //链尾 transient Node<E> last;
2个构造器
//空链表 public LinkedList() { } //根据c创建链表,链表的顺序取决于c的iterator返回元素的顺序 public LinkedList(Collection<? extends E> c) { this(); addAll(c); }
操作
//返回链表第一个元素 public E getFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return f.item; } //返回链表最后一个元素 public E getLast() { final Node<E> l = last; if (l == null) throw new NoSuchElementException(); return l.item; } //删除第一个元素,返回值是第一个元素的值 public E removeFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return unlinkFirst(f); } //删除最后一个元素,返回值是最后一个元素 public E removeLast() { final Node<E> l = last; if (l == null) throw new NoSuchElementException(); return unlinkLast(l); } //插入一个元素作为链头 public void addFirst(E e) { linkFirst(e); } //插入一个元素作为链尾 public void addLast(E e) { linkLast(e); } //判断链表是否含有o public boolean contains(Object o) { return indexOf(o) != -1; } //返回链表大小 public int size() { return size; } //与addLast的效果一样,在链尾添加一个元素 public boolean add(E e) { linkLast(e); return true; } //删除从链头开始遇到第一个与o等同的元素,如果返回true,表示删除成功 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; } //将c集合的元素添加到链表,插入顺序取决于c的iterator返回的元素顺序 public boolean addAll(Collection<? extends E> c) { return addAll(size, c); } //在index位置开始将c集合的元素添加到链表,插入顺序取决于c的iterator返回的元素顺序 public boolean addAll(int index, Collection<? extends E> c) { checkPositionIndex(index); Object[] a = c.toArray(); int numNew = a.length; if (numNew == 0) return false; Node<E> pred, succ; if (index == size) { succ = null; pred = last; } else { succ = node(index); pred = succ.prev; } for (Object o : a) { @SuppressWarnings("unchecked") E e = (E) o; Node<E> newNode = new Node<>(pred, e, null); if (pred == null) first = newNode; else pred.next = newNode; pred = newNode; } if (succ == null) { last = pred; } else { pred.next = succ; succ.prev = pred; } size += numNew; modCount++; return true; } //清空链表 public void clear() { // Clearing all of the links between nodes is "unnecessary", but: // - helps a generational GC if the discarded nodes inhabit // more than one generation // - is sure to free memory even if there is a reachable Iterator for (Node<E> x = first; x != null; ) { Node<E> next = x.next; x.item = null; x.next = null; x.prev = null; x = next; } first = last = null; size = 0; modCount++; } //获取在index位置的元素 public E get(int index) { checkElementIndex(index); return node(index).item; } //将index位置的元素的值修改为element public E set(int index, E element) { checkElementIndex(index); Node<E> x = node(index); E oldVal = x.item; x.item = element; return oldVal; } //在index位置插入一个element元素 public void add(int index, E element) { checkPositionIndex(index); if (index == size) linkLast(element); else linkBefore(element, node(index)); } //删除index位置的元素,返回值是被删除元素的值 public E remove(int index) { checkElementIndex(index); return unlink(node(index)); } //返回从链头到链尾第一个遇到与o等同的元素所在的位置,如果不存在返回-1 public int indexOf(Object o) { int index = 0; if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) return index; index++; } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) return index; index++; } } return -1; } //返回从链尾到链头第一个遇到与o等同的元素所在的位置,如果不存在返回-1 public int lastIndexOf(Object o) { int index = size; if (o == null) { for (Node<E> x = last; x != null; x = x.prev) { index--; if (x.item == null) return index; } } else { for (Node<E> x = last; x != null; x = x.prev) { index--; if (o.equals(x.item)) return index; } } return -1; } //返回链头元素值 public E peek() { final Node<E> f = first; return (f == null) ? null : f.item; } //返回链头元素值,如果链表为空,抛出NoSuchElementException public E element() { return getFirst(); } //返回并删除链头元素值 public E poll() { final Node<E> f = first; return (f == null) ? null : unlinkFirst(f); } //返回并删除链头元素值,如果链表为 空,抛出NoSuchElementException public E remove() { return removeFirst(); } //在链尾添加元素e public boolean offer(E e) { return add(e); } //在链头插入元素e public boolean offerFirst(E e) { addFirst(e); return true; } //在链尾插入元素e public boolean offerLast(E e) { addLast(e); return true; } //返回链头元素,如果链表为空,返回null public E peekFirst() { final Node<E> f = first; return (f == null) ? null : f.item; } //返回链尾元素,如果链表为空,返回null public E peekLast() { final Node<E> l = last; return (l == null) ? null : l.item; } //返回并删除链头元素 public E pollFirst() { final Node<E> f = first; return (f == null) ? null : unlinkFirst(f); } //返回并删除链尾元素 public E pollLast() { final Node<E> l = last; return (l == null) ? null : unlinkLast(l); } //在链头插入元素e public void push(E e) { addFirst(e); } //删除并返回链头元素 public E pop() { return removeFirst(); } //删除从链头到结尾第一个遇到与o等同的元素,如果不存在,什么也不做 public boolean removeFirstOccurrence(Object o) { return remove(o); } //删除从链尾到链头第一个遇到与o等同的元素,如果不存在,什么也不做 public boolean removeLastOccurrence(Object o) { if (o == null) { for (Node<E> x = last; x != null; x = x.prev) { if (x.item == null) { unlink(x); return true; } } } else { for (Node<E> x = last; x != null; x = x.prev) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; } //返回一个列表迭代器,可以向前或向后迭代,index指定起始位置 public ListIterator<E> listIterator(int index) { checkPositionIndex(index); return new ListItr(index); } //返回从链尾开始的迭代器,在ListIterator的基础上实现的 public Iterator<E> descendingIterator() { return new DescendingIterator(); }
支持clone
public Object clone() { LinkedList<E> clone = superClone(); // Put clone into "virgin" state clone.first = clone.last = null; clone.size = 0; clone.modCount = 0; // Initialize clone with our elements for (Node<E> x = first; x != null; x = x.next) clone.add(x.item); return clone; }
支持序列化和反序列化
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { // Write out any hidden serialization magic s.defaultWriteObject(); // Write out size s.writeInt(size); // Write out all elements in the proper order. for (Node<E> x = first; x != null; x = x.next) s.writeObject(x.item); } private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in any hidden serialization magic s.defaultReadObject(); // Read in size int size = s.readInt(); // Read in all elements in the proper order. for (int i = 0; i < size; i++) linkLast((E)s.readObject()); }