ArrayList是實(shí)現(xiàn)了基于動(dòng)態(tài)數(shù)組的數(shù)據(jù)結(jié)構(gòu),LinkedList基于鏈表的數(shù)據(jù)結(jié)構(gòu)。
LinkedList不支持高效的隨機(jī)元素訪問慧耍。
ArrayList的空間浪費(fèi)主要體現(xiàn)在在list列表的結(jié)尾預(yù)留一定的容量空間,而LinkedList的空間花費(fèi)則體現(xiàn)在它的每一個(gè)元素都需要消耗相當(dāng)?shù)目臻g,就存儲(chǔ)密度來說昌腰,ArrayList是優(yōu)于LinkedList的。
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
//實(shí)現(xiàn)Serilizable接口時(shí)膀跌,將不需要序列化的屬性前添加關(guān)鍵字transient遭商,序列化對(duì)象的時(shí)候,這個(gè)屬性就不會(huì)序列化到指定的目的地中捅伤。
transient int size = 0;
//指向首節(jié)點(diǎn)
transient Node<E> first;
//指向最后一個(gè)節(jié)點(diǎn)
transient Node<E> last;
//構(gòu)建一個(gè)空列表
public LinkedList() {
}
//構(gòu)建一個(gè)包含集合c的列表
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
//將節(jié)點(diǎn)值為e的節(jié)點(diǎn)作為首節(jié)點(diǎn)
private void linkFirst(E e) {
final Node<E> f = first;
//構(gòu)建一個(gè)prev值為null,next為f,節(jié)點(diǎn)值為e的節(jié)點(diǎn)
final Node<E> newNode = new Node<>(null, e, f);
//將newNode作為首節(jié)點(diǎn)
first = newNode;
//如果newNode后面沒有節(jié)點(diǎn)就將newNode作為最后一個(gè)節(jié)點(diǎn)
if (f == null)
last = newNode;
//否則就將newNode賦給其prev
else
f.prev = newNode;
//列表長度加一
size++;
modCount++;
}
//將節(jié)點(diǎn)值為e的節(jié)點(diǎn)作為最后一個(gè)節(jié)點(diǎn)
void linkLast(E e) {
final Node<E> l = last;
//構(gòu)建一個(gè)prev值為l,next為null,節(jié)點(diǎn)值為e的節(jié)點(diǎn)
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
//在非空節(jié)點(diǎn)succ之前插入節(jié)點(diǎn)e
void linkBefore(E e, Node<E> succ) {
final Node<E> pred = succ.prev;
//將succ前面的節(jié)點(diǎn)和succ作為其prev和next
final Node<E> newNode = new Node<>(pred, e, succ);
//然后將newNode作為succ的prev
succ.prev = newNode;
//如果原來succ是首節(jié)點(diǎn)劫流,則將newNode設(shè)置為首節(jié)點(diǎn)
if (pred == null)
first = newNode;
else
pred.next = newNode;
size++;
modCount++;
}
//釋放非空的首節(jié)點(diǎn)f
private E unlinkFirst(Node<E> f) {
final E element = f.item;
final Node<E> next = f.next;
f.item = null;
f.next = null; // help GC
//將first節(jié)點(diǎn)后面的節(jié)點(diǎn)設(shè)為首節(jié)點(diǎn)
first = next;
if (next == null)
last = null;
else
next.prev = null;
size--;
modCount++;
return element;
}
//釋放非空的尾節(jié)點(diǎn)l
private E unlinkLast(Node<E> l) {
final E element = l.item;
final Node<E> prev = l.prev;
l.item = null;
l.prev = null; // help GC
last = prev;
if (prev == null)
first = null;
else
prev.next = null;
size--;
modCount++;
return element;
}
//刪除非空節(jié)點(diǎn)x
E unlink(Node<E> x)
{
final E element = x.item;
final Node<E> next = x.next; //x后面的節(jié)點(diǎn)
final Node<E> prev = x.prev; //x前面的節(jié)點(diǎn)
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;
}
//返回列表首節(jié)點(diǎn)元素值
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
//返列表尾節(jié)點(diǎn)元素值
public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}
//移除首節(jié)點(diǎn)
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
//刪除尾節(jié)點(diǎn)
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
//在列表首部插入節(jié)點(diǎn)e
public void addFirst(E e) {
linkFirst(e);
}
//在列表尾部插入節(jié)點(diǎn)e
public void addLast(E e) {
linkLast(e);
}
//判斷列表中是否包含有元素o
public boolean contains(Object o) {
return indexOf(o) != -1;
}
//返回列表長度大小
public int size() {
return size;
}
//在列表尾部插入元素
public boolean add(E e) {
linkLast(e);
return true;
}
//刪除元素為o的節(jié)點(diǎn)
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中所有元素添加到列表的尾部
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
//從指定的位置index開始,將集合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 { //否則丛忆,找到index所在的節(jié)點(diǎn)
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;
}
//刪除列表中所有節(jié)點(diǎn)
public void clear() {
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++;
}
//獲取指定索引位置節(jié)點(diǎn)的元素值
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
//替換指定索引位置節(jié)點(diǎn)的元素值
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
//在指定索引位置之前插入元素e
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
//刪除指定位置的元素
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
//判斷指定索引位置的元素是否存在
private boolean isElementIndex(int index) {
return index >= 0 && index < size;
}
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
}
//構(gòu)建 IndexOutOfBoundsException詳細(xì)信息
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}
private void checkElementIndex(int index) {
if (!isElementIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private void checkPositionIndex(int index) {
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
//返回指定索引位置的節(jié)點(diǎn)
Node<E> node(int index) {
//此處是一個(gè)小技巧祠汇,當(dāng)index<size/2時(shí),從列表前半部分開始熄诡,否則從后半部分開始
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;
}
}//返回列表中第一次出現(xiàn)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;
}
//逆向搜索,返回第一出現(xiàn)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;
}
//獲取列表首節(jié)點(diǎn)元素值
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
//獲取列表首節(jié)點(diǎn)元素值根穷,若為空則拋異常
public E element() {
return getFirst();
}
//檢索首節(jié)點(diǎn),若空則返回null,不為空則返回其元素值并刪除首節(jié)點(diǎn)
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
//檢索首節(jié)點(diǎn)导坟,若空則拋異常,不為空則返回其元素值并刪除首節(jié)點(diǎn)
public E remove() {
return removeFirst();
}
//在列表尾部增加節(jié)點(diǎn)e
public boolean offer(E e) {
return add(e);
}
//在列表首部插入節(jié)點(diǎn)e
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
//在列表尾部插入節(jié)點(diǎn)e
public boolean offerLast(E e) {
addLast(e);
return true;
}
public E peekFirst() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
//獲取列表尾節(jié)點(diǎn)元素值
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);
}
//入棧
public void push(E e)
{
addFirst(e);
}
//出棧
public E pop() {
return removeFirst();
}
//刪除列表中第一出現(xiàn)o的節(jié)點(diǎn)
public boolean removeFirstOccurrence(Object o) {
return remove(o);
}
//逆向搜索屿良,刪除第一次出現(xiàn)o的節(jié)點(diǎn)
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;
}