1元素的存储结构
在LinkedList中,每一个元素都是Node存储,Node拥有一个存储值的item与一个前驱prev和一个后继next,如下:
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
;
}
}
2继承关系
LinkedList继承关系如下图:
3相关属性
transient int size
= 0;
transient Node
<E> first
;
transient Node
<E> last
;
从源码中我们可以看出,LinkedList采用双链表的结构进行存储
4LinkedList构造函数
public LinkedList(Collection
<? extends E> c
) {
this();
addAll(c
);
}
public boolean addAll(Collection
<? extends E> c
) {
return addAll(size
, 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
= 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;
}
5主要方法
5.1 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
++;
}
5.2 add(int index, E element)方法
public void add(int index
, E element
) {
checkPositionIndex(index
);
if (index
== size
)
linkLast(element
);
else
linkBefore(element
, node(index
));
}
private void checkPositionIndex(int index
) {
if (!isPositionIndex(index
))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index
));
}
private boolean isPositionIndex(int index
) {
return index
>= 0 && index
<= size
;
}
void linkBefore(E e
, Node
<E> succ
) {
final Node
<E> pred
= succ
.prev
;
final Node
<E> newNode
= new Node<>(pred
, e
, succ
);
succ
.prev
= newNode
;
if (pred
== null
)
first
= newNode
;
else
pred
.next
= newNode
;
size
++;
modCount
++;
}
5.3 get(int index)方法
public E
get(int index
) {
checkElementIndex(index
);
return node(index
).item
;
}
Node
<E> node(int index
) {
if (index
< (size
>> 1)) {
Node
<E> x
= first
;
for (int i
= 0; i
< index
; i
++)
x
= x
.next
;
return x
;
} else {
Node
<E> x
= last
;
for (int i
= size
- 1; i
> index
; i
--)
x
= x
.prev
;
return x
;
}
5.4 remove(int index)方法
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;
}
6 Duque接口
6.1 addFirst(E e)方法
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
++;
}
5.2 addLast(E e)方法
public void addLast(E e
) {
linkLast(e
);
}
6.3 push(E e)方法
public void push(E e
) {
addFirst(e
);
}
6.4 getFirst()方法
// 作用:得到头元素
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
6.5 getLast()方法
public E
getLast() {
final Node
<E> l
= last
;
if (l
== null
)
throw new NoSuchElementException();
return l
.item
;
}
6.6 peek()方法
public E
peek() {
final Node
<E> f
= first
;
return (f
== null
) ? null
: f
.item
;
}
6.7 peekFirst()方法
public E
peekFirst() {
final Node
<E> f
= first
;
return (f
== null
) ? null
: f
.item
;
}
6.8 peekLast()方法
public E
peekLast() {
final Node
<E> l
= last
;
return (l
== null
) ? null
: l
.item
;
}
6.9 poll()方法
public E
poll() {
final Node
<E> f
= first
;
return (f
== null
) ? null
: unlinkFirst(f
);
}
6.10 pollFirst()方法
public E
pollFirst() {
final Node
<E> f
= first
;
return (f
== null
) ? null
: unlinkFirst(f
);
}
6.11 pollLast()方法
public E
pollLast() {
final Node
<E> l
= last
;
return (l
== null
) ? null
: unlinkLast(l
);
}
6.12 pop()方法
public E
pop() {
return removeFirst();
}
public E
removeFirst() {
final Node
<E> f
= first
;
if (f
== null
)
throw new NoSuchElementException();
return unlinkFirst(f
);
}
push(E e)方法
public void push(E e
) {
addFirst(e
);
}
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
++;
}
后记
其中的关于序列化、Iterator这两块,与ArrayList的实现也是不尽相同的,故在此可参考ArrayList中的解析。