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());
}

最新文章

  1. 1.ASP.NET MVC使用EPPlus,导出数据到Excel中
  2. Java反射机制专题
  3. Python 变量类型
  4. 自己封装的一个java图片验证码
  5. C语言中的const
  6. POJ 1201 Intervals(图论-差分约束)
  7. VC++深入详解-第一章学习心得(二)
  8. Toast——提醒方式
  9. [C++]Store Credit——Google Code Jam Qualification Round Africa 2010
  10. solr4.x设置默认查询字段
  11. Java学习笔记13---如何理解“子类重写父类方法时,返回值若为类类型,则必须与父类返回值类型相同或为其子类”
  12. JS(原生js和jq方式)获取元素属性(自定义属性),删除属性(自定义属性)
  13. 浅谈 RxAndroid + Retrofit + Databinding
  14. 13.Git分支-变基(rebase)、rebase VS merge
  15. windows10下配置环境变量
  16. WRF安装过程
  17. Linux安装Python3后,如何使用pip命令
  18. art-template实战
  19. spring boot cloud
  20. ser2net的编译及测试

热门文章

  1. 检测Insert、Capslock、NumLock、ScrollLock状态键的状态
  2. CSAPP缓冲区溢出攻击实验(上)
  3. 查看修改swap空间大小
  4. mysqldump的流程
  5. 日志分析(五) PV&amp;UV
  6. 打开Excel时提示"您尝试打开的文件**.xls的格式与文件扩展名指定的格式不一致"
  7. C - Minimum Inversion Number
  8. JAVAMAIL手动发送邮件
  9. dem30下载地址
  10. IDL中File_Search函数用法详解(转)