LinkedList source code analysis 2

 		LinkedList<Integer> ls = new LinkedList<>();
        ls.add(1);
        ls.add(3);
        ls.add(5);
        ls.add(7);
        Integer peek = ls.peek();//1
        Integer peekFirst = ls.peekFirst();//1
        Integer peekLast = ls.peekLast();//7
return value function meaning
E peekFirst Returns the first element in this list.
E peek() Returns the first element in this list.
E peekLast () Returns the last element in this list.

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
public void add(int index, E element) {
    
    
        checkPositionIndex(index);

        if (index == size)
        //如果当前插入的位置和LinkedList的长度相等,那么插入尾部
            linkLast(element);
        else
        //否则插入到固定的位置
            linkBefore(element, node(index));
}
 void linkBefore(E e, Node<E> succ) {
    
    
        // assert succ != null;
        //原来位置的结点获取前驱
        final Node<E> pred = succ.prev;
        //创建新的结点。设置前驱为原来结点的前驱,后继为原来的结点
        final Node<E> newNode = new Node<>(pred, e, succ);
        //将新的结点赋值给原结点的前驱
        succ.prev = newNode;
        if (pred == null)
            first = newNode;
        else
            pred.next = newNode;
        size++;
        modCount++;
}
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;
        }
		//如果原来结点为空,那么设置当前pred为最后一个结点。
        if (succ == null) {
    
    
            last = pred;
        } else {
    
    
        //否则。将原来结点这只为待插入结点的下一个结点。当前结点为原来结点的前驱
            pred.next = succ;
            succ.prev = pred;
        }
		
        size += numNew;
        modCount++;
        return true;
    }
//直接将集合插入到原来链表的尾部
public boolean addAll(Collection<? extends E> c) {
    
    
        return addAll(size, c);
}

Insert picture description here

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);
        //获取指定位置的元素
        return node(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() {
    
    
        return getFirst();
}
//获取元素第一次出现位置的索引,没有则返回-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;
    }
//找出元素最后一次出现的位置。从后往前遍历
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;
    }
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;
    }

Guess you like

Origin blog.csdn.net/GoSaint/article/details/114224470