//构造一个空的LinkedList
public LinkedList() {
}
//接收一个Collection参数c,默认构造方法构造一个空的链表,并通过addAll将c中的元素全部添加到链表中。
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
//获取LinkedList第一个元素,如果LinkedList为空,则抛出异常。
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
//获取getLast()最后一个元素,如果LinkedList为空,则抛出异常。
public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}
//删除LinkedList第一个元素,如果LinkedList为空,则抛出异常。
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
//删除LinkedList最后一个元素,如果LinkedList为空,则抛出异常。
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
//在LinkedList开始的位置插入一个新元素。
public void addFirst(E e) {
linkFirst(e);
}
//在LinkedList末尾的位置插入一个新的元素。
public void addLast(E e) {
linkLast(e);
}
//判断LinkedList中是否包含某个元素,若包含返回True,否则返回False
public boolean contains(Object o) {
return indexOf(o) != -1;
}
//在LinkedList末尾处添加一个新的元素。
public boolean add(E e) {
linkLast(e);
return true;
}
//删除LinkedList中第一个出现的指定元素,如果LinkedList中不包含该元素,则链表保持不变。
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;
}
//增加Collection中的所有元素,
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
//index参数指定collection中插入的第一个元素的位置
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;
}
//删除LinkedList中的所有元素。
public void clear() {
//删除LinkedList中所有链接是没有必要的,但是:
// 可以帮助分代GC,如果删除节点超过一代就会释放内存,因为有一个可用的迭代器。
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++;
}
//获取指定位置的节点
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
//给指定节点赋一个指定的值,替换原来的值。
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
//在指定位置插入一个指定的值,原来该位置上以后的节点依次向后移动一位。
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));
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有