一、LinkedList
LinkedList是基于链表结构的一种线性结构存储结构,他和ArrayList的结构类似,两者均可以创建动态数组结构,唯一区别在于ArrayList创建的动态数组在内存上是连续的,而LinkedList是离散的。上一篇文章的超详细的逐句介绍Java高级接口之LinkedList底层源码讲解(一)详细介绍了LinkedList的一些内部方法,下面我将介绍LinkedList的一些使用方法
二、LinkedList的使用方法
下面方法定义了获取第一个元素的方法。
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;
}
在链表最后的位置增加元素
public boolean add(E e) {
linkLast(e);
return true;
}
移除链表中的o元素
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);
}
从指定位置开始,将指定集合中的所有元素插入到此列表中。将当前在该位置的元素(如果有)和任何后续元素向右移动(增加它们的索引)。新元素将按照指定集合的迭代器返回的顺序出现在列表中。
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() {
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;
}
用element来替代index位的元素。
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是否在该链表中,如果在则返回下标
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的位置
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;
}
检索但不删除此列表的头部(第一个元素)。返回: 此列表的头部,如果此列表为空,则返回 null
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
检索但不删除此列表的头部(第一个元素)。返回:这个列表的头部
public E element() {
return getFirst();
}
将第一个元素断开连接
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
移除第一个元素
public E remove() {
return removeFirst();
}
添加指定元素作为此列表的尾部(最后一个元素)
public boolean offer(E e) {
return add(e);
}
在此列表的前面插入指定的元素。参数:e - 要插入的元素
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
在此列表的末尾插入指定的元素
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;
}
检索并删除此列表的第一个元素,如果此列表为空,则返回 null。
public E pollFirst() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
检索并删除此列表的最后一个元素,如果此列表为空,则返回 null。
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();
}
删除此列表中第一次出现的指定元素(从头到尾遍历列表时)。如果列表不包含该元素,则它保持不变。
public boolean removeFirstOccurrence(Object o) {
return remove(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;
}