继承关系
Arraylist是以数组实现集合,而LinkedList是以双向链表实现的集合,LinkedList的继承关系如下:
Linkedlist与Arraylist的区别有:
1.Arraylist直接继承AbstractList,而Linkedlist是继承的AbstractSequentialList
2.Arraylist实现了RandomAccess接口可以直接用foreach遍历,而LinkedList没有实现,因此不能foreach遍历
3.LinedList实现Deque(双向队列接口)
属性
LinkedList的属性主要如下,包含
Node:first 头指针
Node:last 尾指针
size linkedlist长度
transient int size = 0;
/**
* Pointer to first node.
*/
transient Node<E> first;
/**
* Pointer to last node.
*/
transient Node<E> last;
其中Node对象是LinkedList的内部类,是一个双向链表
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
构造方法
构造方法有两个,一个默认的无参构造方法,一个LinkedList(Collection<? extends E> c)
public LinkedList(Collection<? extends E> c) {
//先调用无参构造方法
this();
//然后将结合对象添加到链表
addAll(c);
}
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遍历到index+1,从succ.prev开始插入,最后prev.next = succ
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)
//pred为空表示空链表,直接头指针指向newNode
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;
}
常用方法
add方法
add(E e)
直接插入到链表尾部
public boolean add(E e) {
linkLast(e);
return true;
}
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
add(int index, E element)
指定位置插入,根据index的位置不同分为直接尾节点插入和找到index节点,然后将index.prev的next换成新插入的值,新插入的值的next换成找到的index节点
public void add(int index, E element) {
//检查是否越界
checkPositionIndex(index);
if (index == size)
//index==size直接尾部插入
linkLast(element);
else
//找到index前一个结点插入 linkBefore(E e, Node<E> succ)
linkBefore(element, node(index));
}
addFirst(E e)
直接头部插入,first换成新节点,新节点的next是之前的first
public void addFirst(E e) {
linkFirst(e);
}
private void linkFirst(E e) {
final Node<E> f = first;
final Node<E> newNode = new Node<>(null, e, f);
first = newNode;
if (f == null)
last = newNode;
else
f.prev = newNode;
size++;
modCount++;
}
addLast(E e)
直接调用尾部插入linkLast(e);
public void addLast(E e) {
linkLast(e);
}
Remove方法
remove() 默认删除头节点
public E remove() {
return removeFirst();
}
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
private E unlinkFirst(Node<E> f) {
// assert f == first && f != null;
final E element = f.item;
final Node<E> next = f.next;
f.item = null;
f.next = null; // help GC
first = next;
if (next == null)
last = null;
else
next.prev = null;
size--;
modCount++;
return element;
}
remove(int index) 删除指定index位置
public E remove(int index) {
//判断是否越界,主要因为非线程安全
checkElementIndex(index);
//向unlink方法传入index的节点,然后让index.prev指向index.next
return unlink(node(index));
}
E unlink(Node<E> x) {
// assert x != null;
final E element = x.item;
final Node<E> next = x.next;
final Node<E> prev = x.prev;
if (prev == null) {
first = next;
} else {
prev.next = next;
x.prev = null;
}
if (next == null) {
last = prev;
} else {
next.prev = prev;
x.next = null;
}
x.item = null;
size--;
modCount++;
return element;
}
remove(Object o)删除第一个与O值相同的节点,如果O为null就删除第一为null的节点,可以说明插入的时候支持null
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;
}
Get方法
get(int index)返回node(inde).item值
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
getFirst()头节点不为空直接返回头节点值
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
getLast()尾节点不为空直接返回尾节点值
public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}
Iterator
LinekedList调用的是父类AbstractSequentialList的iterator方法
public Iterator<E> iterator() {
return listIterator();
}
//Arraylist复写的listIterator
public ListIterator<E> listIterator(int index) {
checkPositionIndex(index);
return new ListItr(index);
}
LinkedList返回的是复写的ListItr迭代器
ListItr
ListItr是一个双链表的迭代器
private class ListItr implements ListIterator<E> {
//上次返回的节点
private Node<E> lastReturned;
//下次返回的节点
private Node<E> next;
//下次索引
private int nextIndex;
//期望修改的次数
private int expectedModCount = modCount;
//构造函数
ListItr(int index) {
// assert isPositionIndex(index);
next = (index == size) ? null : node(index);
nextIndex = index;
}
//能否继续遍历
public boolean hasNext() {
return nextIndex < size;
}
//获取值
public E next() {
checkForComodification();
if (!hasNext())
throw new NoSuchElementException();
lastReturned = next;
next = next.next;
nextIndex++;
return lastReturned.item;
}
//能否前序遍历
public boolean hasPrevious() {
return nextIndex > 0;
}
//前序遍历
public E previous() {
checkForComodification();
if (!hasPrevious())
throw new NoSuchElementException();
lastReturned = next = (next == null) ? last : next.prev;
nextIndex--;
return lastReturned.item;
}
//获取下一个索引
public int nextIndex() {
return nextIndex;
}
//获取上一个索引
public int previousIndex() {
return nextIndex - 1;
}
//删除方法
public void remove() {
//校验期待的修改次数与当前修改次数是否一致,不一致表示可能其他线程修改了值需要抛异常
checkForComodification();
if (lastReturned == null)
throw new IllegalStateException();
Node<E> lastNext = lastReturned.next;
unlink(lastReturned);
if (next == lastReturned)
next = lastNext;
else
nextIndex--;
lastReturned = null;
expectedModCount++;
}
//修改值
public void set(E e) {
if (lastReturned == null)
throw new IllegalStateException();
checkForComodification();
lastReturned.item = e;
}
//添加值
public void add(E e) {
checkForComodification();
lastReturned = null;
if (next == null)
linkLast(e);
else
linkBefore(e, next);
nextIndex++;
expectedModCount++;
}
//遍历方法
public void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
while (modCount == expectedModCount && nextIndex < size) {
action.accept(next.item);
lastReturned = next;
next = next.next;
nextIndex++;
}
checkForComodification();
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}