标签:地址 构造 add 不能 getc color ref 输出 类型
转载请注明出处:http://blog.csdn.net/ns_code/article/details/35787253
您好,我正在参加CSDN博文大赛,如果您喜欢我的文章,希望您能帮我投一票,谢谢!
投票地址:http://vote.blog.csdn.net/Article/Details?articleid=35568011
LinkedList简介
LinkedList是基于双向循环链表(从源码中可以很容易看出)实现的,除了可以当做链表来操作外,它还可以当做栈、队列和双端队列来使用。
LinkedList同样是非线程安全的,只在单线程下适合使用。
LinkedList实现了Serializable接口,因此它支持序列化,能够通过序列化传输,实现了Cloneable接口,能被克隆。
LinkedList源码剖析
LinkedList的源码如下(加入了比较详细的注释):
- package java.util;
-
- public class LinkedList<E>
- extends AbstractSequentialList<E>
- implements List<E>, Deque<E>, Cloneable, java.io.Serializable
- {
-
- private transient Entry<E> header = new Entry<E>(null, null, null);
-
-
- private transient int size = 0;
-
-
- public LinkedList() {
- header.next = header.previous = header;
- }
-
-
- public LinkedList(Collection<? extends E> c) {
- this();
- addAll(c);
- }
-
-
- public E getFirst() {
- if (size==0)
- throw new NoSuchElementException();
-
-
-
- return header.next.element;
- }
-
-
- public E getLast() {
- if (size==0)
- throw new NoSuchElementException();
-
-
-
- return header.previous.element;
- }
-
-
- public E removeFirst() {
- return remove(header.next);
- }
-
-
- public E removeLast() {
- return remove(header.previous);
- }
-
-
- public void addFirst(E e) {
- addBefore(e, header.next);
- }
-
-
- public void addLast(E e) {
- addBefore(e, header);
- }
-
-
- public boolean contains(Object o) {
- return indexOf(o) != -1;
- }
-
-
- public int size() {
- return size;
- }
-
-
- public boolean add(E e) {
-
-
- addBefore(e, header);
- return true;
- }
-
-
-
-
- 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;
- }
-
-
-
- public boolean addAll(Collection<? extends E> c) {
- return addAll(size, c);
- }
-
-
- public boolean addAll(int index, Collection<? extends E> c) {
- if (index < 0 || index > size)
- throw new IndexOutOfBoundsException("Index: "+index+
- ", Size: "+size);
- Object[] a = c.toArray();
-
- int numNew = a.length;
- if (numNew==0)
- return false;
- modCount++;
-
-
- Entry<E> successor = (index==size ? header : entry(index));
-
- Entry<E> predecessor = successor.previous;
-
- for (int i=0; i<numNew; i++) {
- Entry<E> e = new Entry<E>((E)a[i], successor, predecessor);
- predecessor.next = e;
- predecessor = e;
- }
- successor.previous = predecessor;
-
-
- size += numNew;
- return true;
- }
-
-
- public void clear() {
- Entry<E> e = header.next;
-
-
-
-
- while (e != header) {
- Entry<E> next = e.next;
- e.next = e.previous = null;
- e.element = null;
- e = next;
- }
- header.next = header.previous = header;
-
- size = 0;
- modCount++;
- }
-
-
- public E get(int index) {
- return entry(index).element;
- }
-
-
- public E set(int index, E element) {
- Entry<E> e = entry(index);
- E oldVal = e.element;
- e.element = element;
- return oldVal;
- }
-
-
- public void add(int index, E element) {
- addBefore(element, (index==size ? header : entry(index)));
- }
-
-
- public E remove(int index) {
- return remove(entry(index));
- }
-
-
- 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)) {
- for (int i = 0; i <= index; i++)
- e = e.next;
- } else {
- for (int i = size; i > index; i--)
- e = e.previous;
- }
- return e;
- }
-
-
-
- public int indexOf(Object o) {
- int index = 0;
- if (o==null) {
- for (Entry e = header.next; e != header; e = e.next) {
- if (e.element==null)
- return index;
- index++;
- }
- } else {
- for (Entry e = header.next; e != header; e = e.next) {
- if (o.equals(e.element))
- return index;
- index++;
- }
- }
- return -1;
- }
-
-
-
- public int lastIndexOf(Object o) {
- int index = size;
- if (o==null) {
- for (Entry e = header.previous; e != header; e = e.previous) {
- index--;
- if (e.element==null)
- return index;
- }
- } else {
- for (Entry e = header.previous; e != header; e = e.previous) {
- index--;
- if (o.equals(e.element))
- return index;
- }
- }
- return -1;
- }
-
-
-
- public E peek() {
- if (size==0)
- return null;
- return getFirst();
- }
-
-
-
- public E element() {
- return getFirst();
- }
-
-
-
- public E poll() {
- if (size==0)
- return null;
- return removeFirst();
- }
-
-
- public boolean offer(E e) {
- return add(e);
- }
-
-
- public boolean offerFirst(E e) {
- addFirst(e);
- return true;
- }
-
-
- public boolean offerLast(E e) {
- addLast(e);
- return true;
- }
-
-
-
- public E peekFirst() {
- if (size==0)
- return null;
- return getFirst();
- }
-
-
-
- public E peekLast() {
- if (size==0)
- return null;
- return getLast();
- }
-
-
-
- public E pollFirst() {
- if (size==0)
- return null;
- return removeFirst();
- }
-
-
-
- public E pollLast() {
- if (size==0)
- return null;
- return removeLast();
- }
-
-
- public void push(E e) {
- addFirst(e);
- }
-
-
- public E pop() {
- return removeFirst();
- }
-
-
-
- public boolean removeFirstOccurrence(Object o) {
- return remove(o);
- }
-
-
-
- public boolean removeLastOccurrence(Object o) {
- if (o==null) {
- for (Entry<E> e = header.previous; e != header; e = e.previous) {
- if (e.element==null) {
- remove(e);
- return true;
- }
- }
- } else {
- for (Entry<E> e = header.previous; e != header; e = e.previous) {
- if (o.equals(e.element)) {
- remove(e);
- return true;
- }
- }
- }
- return false;
- }
-
-
- public ListIterator<E> listIterator(int index) {
- return new ListItr(index);
- }
-
-
- private class ListItr implements ListIterator<E> {
-
- private Entry<E> lastReturned = header;
-
- private Entry<E> next;
-
- private int nextIndex;
-
- private int expectedModCount = modCount;
-
-
-
- ListItr(int index) {
-
- if (index < 0 || index > size)
- throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+size);
-
-
- if (index < (size >> 1)) {
- next = header.next;
- for (nextIndex=0; nextIndex<index; nextIndex++)
- next = next.next;
- } else {
- next = header;
- for (nextIndex=size; nextIndex>index; nextIndex--)
- next = next.previous;
- }
- }
-
-
- public boolean hasNext() {
-
- return nextIndex != size;
- }
-
-
- public E next() {
- checkForComodification();
- if (nextIndex == size)
- throw new NoSuchElementException();
-
- lastReturned = next;
-
- next = next.next;
- nextIndex++;
- return lastReturned.element;
- }
-
-
- public boolean hasPrevious() {
-
- return nextIndex != 0;
- }
-
-
- public E previous() {
- if (nextIndex == 0)
- throw new NoSuchElementException();
-
-
- lastReturned = next = next.previous;
- nextIndex--;
- checkForComodification();
- return lastReturned.element;
- }
-
-
- public int nextIndex() {
- return nextIndex;
- }
-
-
- public int previousIndex() {
- return nextIndex-1;
- }
-
-
-
- public void remove() {
- checkForComodification();
- Entry<E> lastNext = lastReturned.next;
- try {
- LinkedList.this.remove(lastReturned);
- } catch (NoSuchElementException e) {
- throw new IllegalStateException();
- }
- if (next==lastReturned)
- next = lastNext;
- else
- nextIndex--;
- lastReturned = header;
- expectedModCount++;
- }
-
-
- public void set(E e) {
- if (lastReturned == header)
- throw new IllegalStateException();
- checkForComodification();
- lastReturned.element = e;
- }
-
-
- public void add(E e) {
- checkForComodification();
- lastReturned = header;
- addBefore(e, next);
- nextIndex++;
- expectedModCount++;
- }
-
-
- final void checkForComodification() {
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
- }
- }
-
-
-
- private static class Entry<E> {
-
- E element;
-
- Entry<E> next;
-
- Entry<E> previous;
-
-
- Entry(E element, Entry<E> next, Entry<E> previous) {
- this.element = element;
- this.next = next;
- this.previous = previous;
- }
- }
-
-
- 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;
- }
-
-
- private E remove(Entry<E> e) {
- if (e == header)
- throw new NoSuchElementException();
-
- E result = e.element;
- e.previous.next = e.next;
- e.next.previous = e.previous;
- e.next = e.previous = null;
- e.element = null;
- size--;
- modCount++;
- return result;
- }
-
-
- public Iterator<E> descendingIterator() {
- return new DescendingIterator();
- }
-
-
- private class DescendingIterator implements Iterator {
- final ListItr itr = new ListItr(size());
-
-
- public boolean hasNext() {
- return itr.hasPrevious();
- }
-
-
- public E next() {
- return itr.previous();
- }
-
- public void remove() {
- itr.remove();
- }
- }
-
-
-
- public Object[] toArray() {
-
- Object[] result = new Object[size];
- int i = 0;
-
- for (Entry<E> e = header.next; e != header; e = e.next)
- result[i++] = e.element;
- return result;
- }
-
-
- public <T> T[] toArray(T[] a) {
-
-
- if (a.length < size)
- a = (T[])java.lang.reflect.Array.newInstance(
- a.getClass().getComponentType(), size);
-
- int i = 0;
- Object[] result = a;
- for (Entry<E> e = header.next; e != header; e = e.next)
- result[i++] = e.element;
-
- if (a.length > size)
- a[size] = null;
-
- return a;
- }
-
-
-
- public Object clone() {
- LinkedList<E> clone = null;
-
- try {
- clone = (LinkedList<E>) super.clone();
- } catch (CloneNotSupportedException e) {
- throw new InternalError();
- }
-
-
- clone.header = new Entry<E>(null, null, null);
- clone.header.next = clone.header.previous = clone.header;
- clone.size = 0;
- clone.modCount = 0;
-
-
- for (Entry<E> e = header.next; e != header; e = e.next)
- clone.add(e.element);
-
- return clone;
- }
-
-
-
- private void writeObject(java.io.ObjectOutputStream s)
- throws java.io.IOException {
-
- s.defaultWriteObject();
-
-
- s.writeInt(size);
-
-
- for (Entry e = header.next; e != header; e = e.next)
- s.writeObject(e.element);
- }
-
-
-
- private void readObject(java.io.ObjectInputStream s)
- throws java.io.IOException, ClassNotFoundException {
-
- s.defaultReadObject();
-
-
- int size = s.readInt();
-
-
- header = new Entry<E>(null, null, null);
- header.next = header.previous = header;
-
-
- for (int i=0; i<size; i++)
- addBefore((E)s.readObject(), header);
- }
-
- }
几点总结
关于LinkedList的源码,给出几点比较重要的总结:
1、从源码中很明显可以看出,LinkedList的实现是基于双向循环链表的,且头结点中不存放数据,如下图;
2、注意两个不同的构造方法。无参构造方法直接建立一个仅包含head节点的空链表,包含Collection的构造方法,先调用无参构造方法建立一个空链表,而后将Collection中的数据加入到链表的尾部后面。
3、在查找和删除某元素时,源码中都划分为该元素为null和不为null两种情况来处理,LinkedList中允许元素为null。
4、LinkedList是基于链表实现的,因此不存在容量不足的问题,所以这里没有扩容的方法。
5、注意源码中的Entry<E> entry(int index)方法。该方法返回双向链表中指定位置处的节点,而链表中是没有下标索引的,要指定位置出的元素,就要遍历该链表,从源码的实现中,我们看到这里有一个加速动作。源码中先将index与长度size的一半比较,如果index<size/2,就只从位置0往后遍历到位置index处,而如果index>size/2,就只从位置size往前遍历到位置index处。这样可以减少一部分不必要的遍历,从而提高一定的效率(实际上效率还是很低)。
6、注意链表类对应的数据结构Entry。如下;
- private static class Entry<E> {
-
- E element;
-
- Entry<E> next;
-
- Entry<E> previous;
-
-
- Entry(E element, Entry<E> next, Entry<E> previous) {
- this.element = element;
- this.next = next;
- this.previous = previous;
- }
- }
7、LinkedList是基于链表实现的,因此插入删除效率高,查找效率低(虽然有一个加速动作)。
8、要注意源码中还实现了栈和队列的操作方法,因此也可以作为栈、队列和双端队列来使用。
转:【Java集合源码剖析】LinkedList源码剖析
标签:地址 构造 add 不能 getc color ref 输出 类型
原文地址:http://www.cnblogs.com/xuyatao/p/6916291.html