LinkedList源码解析二

4次阅读

共计 4252 个字符,预计需要花费 11 分钟才能阅读完成。

size() 返回结合中存储的节点数量

public int size() {return size;}

add(E e) 向集合末尾添加一个元素

 public boolean add(E e) {linkLast(e);
        return true;
    }

remove(Object o) 移除一个元素

public boolean remove(Object o) {if (o == null) {// 如果 o 是 null
            for (Node<E> x = first; x != null; x = x.next) {// 从第一个节点指针指向的节点开始循环
                if (x.item == null) {// 比较节点的值,的内存地址
                    unlink(x);// 取消节点
                    return true;// 操作成功
                }
            }
        } else {// 如果 o 是不是 null
            for (Node<E> x = first; x != null; x = x.next) {// 从第一个节点指针指向的节点开始循环
                if (o.equals(x.item)) {// 调用 o 的 equals 方法和节点的值作比较
                    unlink(x);// 取消节点
                    return true;// 操作成功
                }
            }
        }
        return false;// 操作失败
    }

addAll(Collection<? extends E> c) 向集合末尾加入集合 c

public boolean addAll(Collection<? extends E> c) {return addAll(size, c);
    }

clear() 清空集合

 public void clear() {
        // Clearing all of the links between nodes is "unnecessary", but:
        // - helps a generational GC if the discarded nodes inhabit
        //   more than one generation
        // - is sure to free memory even if there is a reachable Iterator
        for (Node<E> x = first; x != null;) {// 从 first 指针指向的节点开始循环
            Node<E> next = x.next;// 获取 x 的 next
            x.item = null;// x 的值置空
            x.next = null;// x 的 next 置空
            x.prev = null;// x 的 prev 置空
            x = next;// x 赋值为 next 下一次循环使用
        }
        first = last = null;// 第一个节点指针和最后一个节点的指针置空
        size = 0;// 数据长度 0
        modCount++;// 操作数不清空
    }

get(int index) 获取 index 索引节点数据

public E get(int index) {checkElementIndex(index);
        return node(index).item;
    }

set(int index, E element) 设置 index 索引处的节点位数为 element

public E set(int index, E element) {checkElementIndex(index);//index 在范围内
        Node<E> x = node(index);// 获取索引处的节点
        E oldVal = x.item;// 获取节点旧的值
        x.item = element;// 给节点的值赋值新值
        return oldVal;// 返回旧的值
    }

add(int index, E element) 根据索引插入数据

 public void add(int index, E element) {checkPositionIndex(index);//index 在范围内

        if (index == size)/、如果索引位 index 等于数据长度
            linkLast(element);// 尾插入
        else
            linkBefore(element, node(index));// 否则插入在 index 索引对应节点之前
    }

remove(int index) 移除索引 index 处的数据

 public E remove(int index) {checkElementIndex(index);//index 在范围内
        return unlink(node(index));
    }

isElementIndex(int index) 判断参数是否是现有元素的索引

  private boolean isElementIndex(int index) {return index >= 0 && index < size;}

isPositionIndex(int index) 判断参数是否是现有元素的索引(迭代器或添加操作)

  private boolean isPositionIndex(int index) {return index >= 0 && index < size;}

构造一个 IndexOutOfBoundsException 详细消息

 private String outOfBoundsMsg(int index) {return "Index:"+index+", Size:"+size;}

    private void checkElementIndex(int index) {if (!isElementIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

    private void checkPositionIndex(int index) {if (!isPositionIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

lastIndexOf(Object o) 返回指定元素最后一次出现的索引

 public int lastIndexOf(Object o) {
        int index = size;// 初始下标赋值
        if (o == null) {// o 为 null
            for (Node<E> x = last; x != null; x = x.prev) {//last 指针指向的节点开始向前循环
                index--;
                if (x.item == null)// 节点的值作内存比较
                    return index;// 返回下标
            }
        } else {// o 不为 null
            for (Node<E> x = last; x != null; x = x.prev) {//last 指针指向的节点开始向前循环
                index--;
                if (o.equals(x.item))// 调用 o 的 equals 方法和节点的值比较
                    return index;
            }
        }
        return -1;
    }

peek() 索但不删除此列表的头部 (null 返回 null)

 public E peek() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;// 如果是 null 的话不返回对象,返回 null
    }

element() 检索但不删除此列表的头部 (null 会抛出异常)

public E element() {return getFirst();
    }

getFirst() 返回此列表中的第一个元素 (null 会抛出异常)

public E getFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return f.item;
    }

poll() 检索并删除此列表的头部 (null 返回 null)

 public E poll() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);// 不为 null 时候,删除并返回第一个节点
    }

remove() 检索并删除此列表的头部

  public E remove() {return removeFirst();
    }

offer(E e) 将指定的元素添加为此列表的尾部

public boolean offer(E e) {return add(e);
    }

offerFirst(E e) 在指定列表第一个节点前面插入 e

 public boolean offerFirst(E e) {addFirst(e);
        return true;
    }

offerLast(E e) 在指定列表最后一个节点后面插入 e

  public boolean offerLast(E e) {addLast(e);
        return true;
    }

peekFirst() 检索但不删除此列表的第一个节点 (null 返回 null)

 public E peekFirst() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
     }

peekFirst() 检索但不删除此列表的最后一个节点 (null 返回 null)

 public E peekLast() {
        final Node<E> l = last;
        return (l == null) ? null : l.item;
    }

pollFirst() 检索并删除此列表的第一个节点 (null 返回 null)

public E pollFirst() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }

pollLast() 检索并删除此列表的第最后一个节点 (null 返回 null)

 public E pollLast() {
        final Node<E> l = last;
        return (l == null) ? null : unlinkLast(l);
    }

push(E e) 将元素插入到第一个节点签名

public void push(E e) {addFirst(e);
    }

pop() 移除第一个节点

  public E pop() {return removeFirst();
    }

removeFirstOccurrence(Object o) 删除此中第一次出现的指定元素

public boolean removeFirstOccurrence(Object o) {return remove(o);
    }

removeLastOccurrence(Object o) 删除此中最后一次出现的指定元素

// 和 lastIndexOf 类似, 找到后直接调用 unlink
 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;
    }

正文完
 0