LinkedList源码解析(二)

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

public int size() {
        return size;
    }

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

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()清空集合ui

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)根据索引插入数据code

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;
    }

ListIterator<E> listIterator(int index)返回集合迭代器

public ListIterator<E> listIterator(int index) {
        checkPositionIndex(index);
        return new ListItr(index);
    }

迭代器类ListItr

private class ListItr implements ListIterator<E> {
        private Node<E> lastReturned;//最后返回的节点
        private Node<E> next;//下一个节点
        private int nextIndex;//下一个节点的索引
        private int expectedModCount = modCount;

        ListItr(int index) {
            // assert isPositionIndex(index);
            next = (index == size) ? null : node(index);//构造下一个节点的索引
            nextIndex = index;
        }

        public boolean hasNext() {
            return nextIndex < size;//判断是否有下一个节点
        }

        public E next() {
            checkForComodification();//线程安全
            if (!hasNext())
                throw new NoSuchElementException();//迭代器到尾部

            lastReturned = next;//迭代器越过next
            next = next.next;//next赋值为next的下一个节点
            nextIndex++;//下一个节点的索引+1
            return lastReturned.item;//返回迭代器越过节点的值
        }

        public boolean hasPrevious() {
            return nextIndex > 0;//是否有前一个节点
        }

        public E previous() {
            checkForComodification();//线程安全
            if (!hasPrevious())
                throw new NoSuchElementException();//迭代器到达头部

            lastReturned = next = (next == null) ? last : next.prev;//若是是空返回last指针指向的节点(不理解)
            nextIndex--;//下一个节点索引自减
            return lastReturned.item;//返回迭代器越过节点的值
        }

        public int nextIndex() {
            return nextIndex;//返回下一个索引
        }

        public int previousIndex() {
            return nextIndex - 1;//返回上一个索引
        }

        public void remove() {
            checkForComodification();
            if (lastReturned == null)//迭代器没有越过任何元素
                throw new IllegalStateException();

            Node<E> lastNext = lastReturned.next;//获取迭代器越过节点的下一个节点
            unlink(lastReturned);//移除越过的元素
            if (next == lastReturned)//不理解为何会进去
                next = lastNext;
            else
                nextIndex--;//下一个节点索引自减
            lastReturned = null;
            expectedModCount++;
        }

        public void set(E e) {
            if (lastReturned == null)//迭代器没有越过任何元素
                throw new IllegalStateException();
            checkForComodification();//线程安全
            lastReturned.item = e;//迭代器越过节点的值
        }

        public void add(E e) {
            checkForComodification();//线程安全
            lastReturned = null;
            if (next == null)//尾巴插入
                linkLast(e);
            else
                linkBefore(e, next);//next节点前插入
            nextIndex++;//下一个节点的索引加1
            expectedModCount++;
        }

        public void forEachRemaining(Consumer<? super E> action) {
            Objects.requireNonNull(action);
            while (modCount == expectedModCount && nextIndex < size) {//下一个节点的索引小于节点数
                action.accept(next.item);//运行accept方法
                lastReturned = next;
                next = next.next;
                nextIndex++;
            }
            checkForComodification();//线程安全
        }

        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }

descendingIterator()适配器经过ListItr.previous提供降序迭代器

public Iterator<E> descendingIterator() {
        return new DescendingIterator();
    }

降序迭代器DescendingIterato(调用的就是ListItr,反着调用)

private class DescendingIterator implements Iterator<E> {
        private final ListItr itr = new ListItr(size());
        public boolean hasNext() {
            return itr.hasPrevious();
        }
        public E next() {
            return itr.previous();
        }
        public void remove() {
            itr.remove();
        }
    }

superClone()超类复制

@SuppressWarnings("unchecked")
private LinkedList<E> superClone() {
        try {
            return (LinkedList<E>) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new InternalError(e);
        }
    }

clone()复制集合对象

public Object clone() {
        LinkedList<E> clone = superClone();

        // Put clone into "virgin" state
        clone.first = clone.last = null;//第一个节点和最后一个节点置空
        clone.size = 0;//数据数置0
        clone.modCount = 0;//操做数置0

        // Initialize clone with our elements
        for (Node<E> x = first; x != null; x = x.next)//从first节点开始循环初始化clone对象
            clone.add(x.item);

        return clone;
    }

toArray()返回集合元素组成的数组

public Object[] toArray() {
        Object[] result = new Object[size];
        int i = 0;
        for (Node<E> x = first; x != null; x = x.next)
            result[i++] = x.item;
        return result;
    }

toArray(T[] a)返回集合元素组成的数组(传入数组的类型)

@SuppressWarnings("unchecked")
    public <T> T[] toArray(T[] a) {
        if (a.length < size)
            a = (T[])java.lang.reflect.Array.newInstance(
                                a.getClass().getComponentType(), size);//建立数组
        int i = 0;
        Object[] result = a;
        for (Node<E> x = first; x != null; x = x.next)
            result[i++] = x.item;

        if (a.length > size)
            a[size] = null;

        return a;
    }
相关文章
相关标签/搜索