下面我们看一下新增一个节点,在集合中的直接视图情况:

假设我们一开始创建一个LinkedList时,只有一个header节点(不存储数据的),如下图所示:

有一个元素A1插入到了header的前面。
现在要插入一个元素A2,在执行完下面代码后,
Entry<E> newEntry = new Entry<E>(A2, header, header.previous); //将newEntry的next属性指向header节点,newEntry.previous属性指向header.previous指向的节点(A1);
newEntry.previous.next = newEntry; //将newEntry.previous节点(A1)的next属性指向newEntry,即将A1.previous属性指向A2。
newEntry.next.previous = newEntry;//将newEntry.next节点(header)的previous属性指向newEntry,即将header.previous属性指向A2.
图形变成了下面的样子:

我们看一下LinkedList的一个带参的构造函数:
/**
*以指定集合c的迭代器返回的节点顺序,构造一个包含指定集合c中所有元素的集合
*/
public LinkedList(Collection<? extends E> c) {
//这里this()调用了LinkedList的无参构造器,使header.next=header.previous=header,即此时仅有一个header节点
this();
//调用addAll(c)方法
addAll(c);
}
/**
*将指定集合c中所有的元素,按照其迭代器返回的顺序全部追加到集合的结尾。
*/
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
/**
*将指定集合c中所有的元素,按照其迭代器返回的顺序,从指定的位置index开始全部插入到集合中。
*/
public boolean addAll(int index, Collection<? extends E> c) {
//对参数index做边界检查,无效抛出IndexOutOfBoundsException
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size);
//将集合c 转换为数组
Object[] a = c.toArray();
//获取数组的长度
int numNew = a.length;
//若数组的长度为0,说明数组是空的,没有可以插入的元素,则直接返回false
if (numNew==0)
return false;
//程序走到这,说明有可以插入的数据了,集合大小肯定会改变,因此modCount+1
modCount++;
//如果入参index==size,则将header节点设置为后继节点successor,否则调用entry(int)方法求出位置index的节点,并将该节点设置为后继节点successor.
Entry<E> successor = (index==size ? header : entry(index));
//获取后继节点successor的前驱节点predecessor
Entry<E> predecessor = successor.previous;
//循环数组中的元素,进入数据的插入
for (int i=0; i<numNew; i++) {
//创建一个新节点e,将数组a的第i个元素a[i]作为新节点e的element属性,successor作为e的next节点,predescessor作为e的previous节点
Entry<E> e = new Entry<E>((E)a[i], successor, predecessor);
//将predecessor的next属性指向新节点e
predecessor.next = e;
//由于还要进行后续的插入,因此将新节点e设置为前驱节点predecessor,以便继续循环
predecessor = e;
}
//在循环数组中的元素插入完成后,将sucessor.previous属性指向predecessor节点,此时已经完成了双向链表的闭环了。
successor.previous = predecessor;
//将集合大小size 增加numNew个
size += numNew;
return true;
}
/**
*返回指定位置index的节点
*/
private Entry<E> entry(int index) {
//对参数index做边界检查,无效抛出IndexOutOfBoundsException
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size);
//将头结点header设置为e
Entry<E> e = header;
//如果入参index小于数组大小size的一半,即index< size/2的话,则从前往后查找(从下标为0开始,到index),否则从后往前查找(从下标为size开始,到index+1)
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;
}
其他的添加操作:
/**
*追加指定元素e到集合的结尾,效果和调用add(E e)方法是一样的(都是调用方法addBefore(e,header)),只是该方法没返回值
*/
public void addLast(E e) {
addBefore(e, header);
}
/**
*将指定元素e插入到集合的开始位置,调用方法addBefore(e,header.next)实现
*/
public void addFirst(E e) {
//这里插入在header.next节点的前面,因此可以认为是集合的开始位置
addBefore(e, header.next);
}
/**
*在指定位置index处插入指定的元素e
*/
public void add(int index, E element) {
//若index== size,即要追加到集合的结尾处,即插入到header之前;否则调用entry(int)方法获取index处的节点,插入到该节点之前
addBefore(element, (index==size ? header : entry(index)));
}
/**
*将元素element设值到位置为index处(即将原index处的值替换为element),并返回原来index处的值
*/
public E set(int index, E element) {
Entry<E> e = entry(index);
E oldVal = e.element;
e.element = element;
return oldVal;
}
/**
*追加元素e到集合结尾
*/
public boolean offer(E e) {
return add(e);
}
/**
*将元素e插入集合的开始位置,调用addFirst(E e)方法实现
*/
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
/**
*将元素e插入集合的结束位置,调用addLast(E e)方法实现
*/
public boolean offerLast(E e) {
addLast(e);
return true;
}
/**
*将元素e推入(push)进入栈 (LinkedList也可以当做栈使用)
*/
public void push(E e) {
addFirst(e);
}
删除操作:
/**
*删除指定元素在集合中的第一个出现的节点(下标index最小的)
*/
public boolean remove(Object o) {
//分为两种情况:1.入参o 为null,使用== 比较 2.入参o不为null,使用equals比较
//若o == null
if (o==null) {
//从前往后开始遍历(从header节点的下一个节点开始)
for (Entry<E> e = header.next; e != header; e = e.next) {
//使用 == 比较
if (e.element==null) {
//找到第一个为null的节点,调用方法remove(Entry e)删除该节点
remove(e);
//返回true,表示删除成功
return true;
}
}
} else {
//从前往后开始遍历(从header节点的下一个节点开始)
for (Entry<E> e = header.next; e != header; e = e.next) {
//使用 equals 比较
if (o.equals(e.element)) {
//找到第一个equals为true的节点,调用方法remove(Entry e)删除该节点
remove(e);
//返回true,表示删除成功
return true;
}
}
}
//返回false,表示没有找到要删除的元素o
return false;
}
/**
*删除指定的节点e(该方法是私有的,供LinkedList内部调用,不对外提供),与ArrayList的删除操作而言,LinkedList的删除操作不需要进行���组的移动,而是仅仅改变下被删除元素的前后两元素的指向而已,因此LinkedList删除操作效率更高。
*/
private E remove(Entry<E> e) {
//判断节点是否是头结点header,我们知道header节点不存储数据,若入参e被发现实header节点,则抛出NoSuchElementException异常。
if (e == header)
throw new NoSuchElementException();
//获取节点e的存储的数据result
E result = e.element;
//将节点e的前一个节点的next属性直接指向e的下一个节点(断开e.previous.next=e这个链条)
e.previous.next = e.next;
//将节点e的下一个节点的previous属性直接指向e的前一个节点(断开e.next.previous=e这个链条)
e.next.previous = e.previous;
//将e的next属性和previous属性都设置为null(断开e对前一个节点和后一个节点的指向的链条)
e.next = e.previous = null;
//将e的本身存储的数据元素element置为null
e.element = null;
//size 大小减一
size--;
//由于remove操作影响了集合的结构(大小),因此modCount+1
modCount++;
//返回被删除节点的存储数据result
return result;
}
清除集合中的所有节点clear()方法:
/**
*清除集合中所有的节点
*/
public void clear() {
//获取header节点的下一个节点e
Entry<E> e = header.next;
//只要遍历一遍集合即可
while (e != header) {
//e节点的下一个节点next
Entry<E> next = e.next;
//将节点e的next属性和previous属性都设置为null(不指向任何节点)
e.next = e.previous = null;
//将节点e的存储数据元素置为null
e.element = null;
//将next节点设置为当前循环的节点e
e = next;
}
//循环删除了集合中的所有有数据的元素后,只保留header节点(不存储数据),并组成一个闭环
header.next = header.previous = header;
//由于清理了所有的元素,此时size 设置为0
size = 0;
//此操作涉及到了集合结构大小的改变,因此modCount + 1
modCount++;
}
查找元素在集合中的下标indexOf()方法和lastIndexOf()
/**
*返回元素o在集合中首次出现的位置下标
*/
public int indexOf(Object o) {
//维护一个位置索引index用了记录变量了多少个元素,从0开始
int index = 0;
//若o == null,则使用 == 比较,从前往后
if (o==null) {
for (Entry e = header.next; e != header; e = e.next) {
if (e.element==null)
//找到了第一个为null的,则返回其下标index
return index;
//在当前循环中没有找到,则将下标index+1
index++;
}
} else {
//若o不为 null,则使用 equals 比较,从前往后
for (Entry e = header.next; e != header; e = e.next) {
if (o.equals(e.element))
//找到了第一个equals为truel的,则返回其下标index
return index;
//在当前循环中没有找到,则将下标index+1
index++;
}
}
//遍历完整个链表没有找到的话,返回-1
return -1;
}
/**
*返回元素o在集合中最后出现的位置下标
*/
public int lastIndexOf(Object o) {
//维护一个位置索引index用了记录变量了多少个元素,从最大size开始
int index = size;
//若o == null,则使用 == 比较,从后往前
if (o==null) {
for (Entry e = header.previous; e != header; e = e.previous) {
//先将index-1
index--;
if (e.element==null)
//找到了第一个遇见为null的,则返回其下标index
return index;
}
} else {
//若o != null,则使用 equals 比较,从后往前
for (Entry e = header.previous; e != header; e = e.previous) {
//先将index-1
index--;
if (o.equals(e.element))
//找到了第一个遇见equals为truel的,则返回其下标index
return index;
}
}
//在遍历集合一遍之后,没有找到元素的,返回-1
return -1;
}
判断集合是否包含某个元素:
/**
*判断集合是否包含指定元素o,调用indexOf(Object o)来实现的
*/
public boolean contains(Object o) {
return indexOf(o) != -1;
}
/**
*获取指定位置index的元素,调用entry(int )来实现,entry(int)这个方法上面已讲过
*/
public E get(int index) {
return entry(index).element;
}
将集合转换为数组:
/**
*按照集合中从前往后的顺序,返回一个包含集合中所有元素的数组
*/
public Object[] toArray() {
//创建一个同集合大小一样的Object数组result
Object[] result = new Object[size];
//维护一个下标索引i,用以表示数组 元素的下标
int i = 0;
//从前往后遍历链表
for (Entry<E> e = header.next; e != header; e = e.next)
//将遍历节点e的存储数据放到数组result中的第i个位置,然后i+1
result[i++] = e.element;
//返回result数组
return result;
}
/**
*给一个数组a,返回一个数组(数组元素按照集合从前往后的顺序排列),该数组包含了集合中的所有元素
*/
public <T> T[] toArray(T[] a) {
//若数组a的长度不足以装入所有的集合元素,则使用Array.newInstance()这一方法创建一个size大小,元素类型为数组a的元素类型的数组,并将该数组赋值给a
if (a.length < size)
a = (T[])Java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);
int i = 0;
Object[] result = a;
//从前往后遍历链表,依次将集合中的元素放入数组a中
for (Entry<E> e = header.next; e != header; e = e.next)
//将遍历节点e的存储数据放到数组result中的第i个位置,然后i+1
result[i++] = e.element;
//若数组a的长度超过了size,则将数组a中size下标置为null
if (a.length > size)
a[size] = null;
//返回数组a
return a;
}
LinkedList的迭代器:
/**
*返回一个list的迭代器,直接new了一个内部类ListItr来实现的
*/
public ListIterator<E> listIterator(int index) {
return new ListItr(index);
}
/**
*ListItr是LinkedList的私有内部类,实现了ListIterator接口
*/
private class ListItr implements ListIterator<E> {
//将header设置为最近一次返回的节点
private Entry<E> lastReturned = header;
//下一次要返回的节点
private Entry<E> next;
//下次要返回节点的下标
private int nextIndex;
//将目前修改集合结构大小的记录数赋值给expectedModCount ,用于比较在一个线程遍历集合时,是否有其他的线程在同步修改该集合结构
private int expectedModCount = modCount;
//入参为int的构造函数
ListItr(int index) {
//对入参进行边界检查,如无效则抛出IndexOutOfBoundsException
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size);
//若index < size/2,则从前往后遍历nextIndex从0到index-1
if (index < (size >> 1)) {
//将header节点的next属性赋值给next,即下一次要返回的节点
next = header.next;
//获取指定位置的节点
for (nextIndex=0; nextIndex<index; nextIndex++)
next = next.next;
} else {
//若index >= size/2,则从后往前遍历nextIndex从size到index
next = header;
//获取指定位置的节点
for (nextIndex=size; nextIndex>index; nextIndex--)
next = next.previous;
}
}
//根据nextIndex是否等于size来判断是否还有没有遍历的节点
public boolean hasNext() {
return nextIndex != size;
}
//获取下一个元素
public E next() {
//检查本集合在遍历的过程中,是否有其他线程对本集合的结构大小做了修改
checkForComodification();
//nextIndex == size,说明链表已经被遍历了一遍,不存在没有被遍历的节点了
if (nextIndex == size)
throw new NoSuchElementException();
//将next节点设置为最近一次返回的节点
lastReturned = next;
//将next节点往后移动一位
next = next.next;
//将nextIndex+1
nextIndex++;
//返回最近一次返回节点的数据元素
return lastReturned.element;
}
//从后往前遍历的过程中,判断是否还有未被遍历的元素
public boolean hasPrevious() {
return nextIndex != 0;
}
//获取上一个元素
public E previous() {
//若nextIndex==0,说明在从后往前遍历(下边从size 到 0)过程中,已经遍历到头了,不存在没有被遍历的节点了,则抛出NoSuchElementException
if (nextIndex == 0)
throw new NoSuchElementException();
//将next往前移动一位,并将next节点赋值给最近返回的节点lastReturned
lastReturned = next = next.previous;
//将nextIndex-1
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++;
}
//将当前迭代器持有的元素 替换为元素e
public void set(E e) {
if (lastReturned == header)
throw new IllegalStateException();
checkForComodification();
lastReturned.element = e;
}
//在当前节点后面插入元素e
public void add(E e) {
checkForComodification();
lastReturned = header;
addBefore(e, next);
nextIndex++;
expectedModCount++;
}
//检查本集合在遍历的过程中,是否有其他线程对本集合的结构大小做了修改,如果别的线程对集合做出了修改,则抛出ConcurrentModificationException
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
/**
*接口ListIterator,继承了迭代器接口Iterator
*/
public interface ListIterator<E> extends Iterator<E> {
//在遍历集合时(按照从前往后的顺序),是否还存在没有遍历的元素
boolean hasNext();
//返回下一个元素
E next();
//在遍历集合时(按照从后往前的顺序),是否还存在没有遍历的元素
boolean hasPrevious();
//返回上一个元素
E previous();
//返回下一个元素的位置下标
int nextIndex();
//返回上一个元素的位置下标
int previousIndex();
//删除正在遍历的元素
void remove();
//将正在遍历的元素替换为 元素e
void set(E e);
//插入元素e
void add(E e);
}
使用LinkedList的注意事项:
1.LinkedList是基于双向链表实现的。
2.LinkedList在插入元素时,必须创建Entry对象,并修改相应元素的前后元素的引用;在查找元素时,必须遍历链表;在删除元素时,遍历链表找到要删除的元素,修改被删除元素的前后元素的引用;
3.LinkedList不是线程安全的。
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-03/129548.htm
