快速导航
LinkedList源码(remove)解析:
-
import
java.util.*;
-
public
class
LinkedListRemove
{
-
public
static
void
main
(String[] args)
{
-
LinkedList
list
=
new
LinkedList();
-
list
.add(
1
);
-
list
.add(
2
);
-
list
.add(
3
);
-
list
.remove();
//默认删除的是第1个元素
-
System.out.println(
list
);
//输出结果为: [2, 3]
-
-
}
-
}
// unlinkFirst()方法详解
-
private E unlinkFirst(Node f) {
-
//
assert f == first && f != null;
-
1.取出item 至 element
-
final E element = f.item;
-
-
2.让
next 指向 f.
next
-
final LinkedList.Node
next = f.
next;
-
-
3.将f.item , f.
next 置空
-
f.item = null;
-
f.
next = null; // help GC
-
-
4.让first指向
next
-
first =
next;
-
-
5.此时的
next不为null,所以执行的是
else中的语句
-
if (
next == null)
-
last = null;
-
else
-
将
next.prev置空,此时的f结点就会被虚拟机回收
-
next.prev = null;
-
size
--;
-
modCount++;
-
return element;
-
}