Judging from the above running results, the first element and the last element of the linked list are obtained respectively. Look at the source code analysis as follows:
// 将首结点赋值给当前结点,如果为空,返回空,否则返回元素public E peek(){
final Node<E> f = first;return(f == null)? null : f.item;}//和peek()函数一样public E peekFirst(){
final Node<E> f = first;return(f == null)? null : f.item;}//将尾结点赋值给当前结点,如果为空,返回空,否则返回元素public E peekLast(){
final Node<E> l = last;return(l == null)? null : l.item;}
Follow the previous article and the current api analysis. The remaining APIs are as follows:
return value
function
meaning
void
add(int index, E element)
Insert the specified element into the specified position in this list
boolean
addAll(int index, Collection<? extends E> c)
Starting from the specified position, insert all elements in the specified collection into this list
boolean
addAll(Collection<? extends E> c)
Append all elements in the specified collection to the end of this list in the order returned by the iterator of the specified collection
publicvoidadd(int index, E element){
checkPositionIndex(index);if(index == size)//如果当前插入的位置和LinkedList的长度相等,那么插入尾部linkLast(element);else//否则插入到固定的位置linkBefore(element,node(index));}voidlinkBefore(E e, Node<E> succ){
// assert succ != null;//原来位置的结点获取前驱final Node<E> pred = succ.prev;//创建新的结点。设置前驱为原来结点的前驱,后继为原来的结点final Node<E> newNode =newNode<>(pred, e, succ);//将新的结点赋值给原结点的前驱
succ.prev = newNode;if(pred == null)
first = newNode;else
pred.next = newNode;
size++;
modCount++;}publicbooleanaddAll(int index, Collection<?extendsE> c){
checkPositionIndex(index);
Object[] a = c.toArray();//获取集合的长度int numNew = a.length;if(numNew ==0)returnfalse;//待插入的结点
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 =newNode<>(pred, e, null);if(pred == null)
first = newNode;else
pred.next = newNode;
pred = newNode;}//如果原来结点为空,那么设置当前pred为最后一个结点。if(succ == null){
last = pred;}else{
//否则。将原来结点这只为待插入结点的下一个结点。当前结点为原来结点的前驱
pred.next = succ;
succ.prev = pred;}
size += numNew;
modCount++;returntrue;}//直接将集合插入到原来链表的尾部publicbooleanaddAll(Collection<?extendsE> c){
returnaddAll(size, c);}
return value
function
meaning
E
get(int index)
Returns the element at the specified position.
E
element()
Retrieve but not delete the head (first element) of this list.
int
indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list; if this list does not contain the element, it returns -1.
int
lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list; if this list does not contain the element, it returns -1.
public E get(int index){
checkElementIndex(index);//获取指定位置的元素returnnode(index).item;}//内部调用node(index)方法
Node<E>node(int index){
//如果index小于LinkedList的1/2.从头往后查找。否则从后往前查找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;}}//获取第一个元素public E element(){
returngetFirst();}//获取元素第一次出现位置的索引,没有则返回-1publicintindexOf(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;}//找出元素最后一次出现的位置。从后往前遍历publicintlastIndexOf(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;}
return value
function
meaning
E
set(int index, E element)
Replace the element at the specified position in this list with the specified element
//修改指定位置的元素public E set(int index, E element){
checkElementIndex(index);//获取原来的结点
Node<E> x =node(index);//获取原来结点的值
E oldVal = x.item;//将新值赋值给原来结点值的引用
x.item = element;//返回旧值return oldVal;}