public class LinkedList<E>
extends AbstractSequentialList<E>//其实AbstractSequentialList已经实现了list接口只是这里的list有更加清晰的作用
implements List<E>, Deque<E>, Cloneable, java.io.Serializable//Deque定义了双端队列的操作
{
private transient Entry<E> header = new Entry<E>(null, null, null);//头结点,上一个索引和下一个索引都指向空,说明是可以循环遍历的
private transient int size = 0;//链表里面的节点个数
/**
* 构造方法1:构造一个空链表
*/
public LinkedList() {
header.next = header.previous = header;//指向自己
}
/**
* 构造方法2:构造一个链表,参数为一个集合Collection,并调用第一个构造方法构造一个空链表,并使用addAll()方法将c全部放进去
*/
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
//最后一个就是header的前一个
public E getLast() {
if (size==0)
throw new NoSuchElementException();
return header.previous.element;
}
public boolean contains(Object o) {//调用了indexOf()方法,因为indexOf方法返回包含值得索引,返回-1就是没有
return indexOf(o) != -1;
}
public boolean remove(Object o) {//删除方法,在这里会调用参数类型为Entry的remove方法
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;
}
/**
* 第二个构造函数被调用,他相应的调用addAll的另一个重构的有size参数的addAll方法
*/
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
/**
*index是第一个元素插入的位置,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();//调用集合的toArray方法,集合的元素放进新创建的Object[]数组
int numNew = a.length;//获取数组长度,也是元素个数
if (numNew==0)//这是错误检查
return false;
modCount++;//这个更改链表的修改的次数
Entry<E> successor = (index==size ? header : entry(index));//标记当前要插入节点的后一个节点,要插入的位置就是的尺寸的话,就把头结点赋值给它,他跟header一样指向同一个节点
Entry<E> predecessor = successor.previous;//当前要插入的节点的下一个节点,因为header.previous就是header,所以predecessor还是指向头结点
for (int i=0; i<numNew; i++) {
Entry<E> e = new Entry<E>((E)a[i], successor, predecessor);
predecessor.next = e;//链表的正相连接
predecessor = e;//讲predecessor移位,指向下次要插入的节点的上一个节点
}
successor.previous = predecessor;//最后的修补工作
size += numNew;//修改链表尺寸
return true;
}
public void clear() {//e指向当前需要清空的节点,从第一个开始,也就是header的后一个,当e为header时,说明已经清理完成。遍历清空方便gc
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++;
}
/**
* 返回索引出的节点
*/
private Entry<E> entry(int index) {
if (index < 0 || index >= size)//错误检测
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size);
Entry<E> e = header;//折半查找,如果index在size前半段就从前面开始,否则反之,提高效率
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) {//o为entry里面的值,查找o第一次出现的位置,所以index从0开始
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) {//查找o最后一次出现的位置,右后向前遍历,所以index从size开始
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 E remove() {
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) {//获取一个ListItr
return new ListItr(index);
}
private class ListItr implements ListIterator<E> {//这是一个类不是表示迭代器,参数index相当于构建一个指针,指向index的位置
//当前的节点,也就是上次工作完后的越过的一个节点
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+
//把next指向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;
}
//时候有上一个节点</span>
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;
}
// 删除双向链表中的当前节点</span>
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;
}
// 将e添加到当前节点的前面</span>
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;
}
//这是remove最底层的方法
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 clone() {//调用父类的clone()再然后创建一个空链表,然后在把节点逐个放进去
LinkedList<E> clone = null;
try {
clone = (LinkedList<E>) super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
// Put clone into "virgin" state
clone.header = new Entry<E>(null, null, null);
clone.header.next = clone.header.previous = clone.header;
clone.size = 0;
clone.modCount = 0;
// Initialize clone with our elements
for (Entry<E> e = header.next; e != header; e = e.next)
clone.add(e.element);
return clone;
}
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) {//如果a的长度小于siz(不能容纳下)用反射创建一个新的数组大小为size,由result引用,向其遍历加入节点后,
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)//如多比size大,其余部分补空
a[size] = null;
return a;
}
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 (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 {
// Read in any hidden serialization magic
s.defaultReadObject();
// Read in size
int size = s.readInt();
// Initialize header
header = new Entry<E>(null, null, null);
header.next = header.previous = header;
// Read in all elements in the proper order.
for (int i=0; i<size; i++)
addBefore((E)s.readObject(), header);
}
}
一个学生对Linkedlist源码分析注释
最新推荐文章于 2020-09-07 17:45:47 发布