• 一天一个类,一点也不累 之 LinkedList


    我们的口号是,一天一个类,一点也不累 。。

    今天要讲的是---LinkedList

    首先,还是看看他的组织结构

    Class LinkedList<E>
    
        java.lang.Object
            java.util.AbstractCollection<E>
                java.util.AbstractList<E>
                    java.util.AbstractSequentialList<E>
                        java.util.LinkedList<E>
    
        Type Parameters:
            E - the type of elements held in this collection
    
        All Implemented Interfaces:
            Serializable, Cloneable, Iterable<E>, Collection<E>, Deque<E>, List<E>, Queue<E>
    
    
        public class LinkedList<E>
        extends AbstractSequentialList<E>
        implements List<E>, Deque<E>, Cloneable, Serializable

    相比上两次介绍的两个类,他的实现接口多了一个Queue & Deque(一个是队列一个是双端队列)

    既然这个也实现了List,同样具有List的一些性质,不知大家是否还记得List中的对象容许为NULL.

    在API的官网上面明确说了一句:【Note that this implementation is not synchronized.】 也就是说LinkedList也是非线程安全的。

    和ArrayList一样,库中存在线程安全的实现方法:

      List list = Collections.synchronizedList(new LinkedList(...));

    所以在非线程安全的容器里面会有一个用来记录该对象改变次数的计数器modCount.

    【首先声明一下,这个LinkedList里面维持这两个对象,一个是first,一个是last,他们的数据类型是Node的内部类。

      

        private static class Node<E> {
            E item;
            Node<E> next;
            Node<E> prev;
    
            Node(Node<E> prev, E element, Node<E> next) {
                this.item = element;
                this.next = next;
                this.prev = prev;
            }
        }

    1、构造方法:

      LinkedList()

        Constructs an empty list.

      LinkedList(Collection<? extends E> c)

        Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
     
    2、首先特别解释一下,transient,这样的对象在行进序列话的时候,  是不被需序列化的,是临时的。
       因为在读源码的时候经常会出现这个关键字。
     
    3、因为这个是链表,所以提供了一些关于链表的操作。
      e.g. getFirst() getLast()。。。。。。
     
    4、值得注意的是从1.6开始,新增了一项称道的方法(虽然自己可以方便实现),这就是能够生成倒序的迭代器。
     1 /**
     2      * Adapter to provide descending iterators via ListItr.previous
     3      */
     4     private class DescendingIterator implements Iterator<E> {
     5         private final ListItr itr = new ListItr(size());
     6         public boolean hasNext() {
     7             return itr.hasPrevious();
     8         }
     9         public E next() {
    10             return itr.previous();
    11         }
    12         public void remove() {
    13             itr.remove();
    14         }
    15     }

    这样以来就满足了在特定场合的需要。

    哇喔,这个类要说的好少啊!~~~

    不再说的多少,而在多思多练。

    中国有句谚语:

            不闻不若闻之,闻之不若见之,见之不若知之,知之不若行之。

  • 相关阅读:
    1061 Dating (20 分)
    1042 Shuffling Machine (20 分)简单模拟
    1132 Cut Integer (20 分)
    1100 Mars Numbers (20 分)
    1077 Kuchiguse (20 分)求字符串最长相同后缀
    1065 A+B and C (64bit) (20 分)大数 溢出
    1107 Social Clusters (30 分)并查集
    1079 Total Sales of Supply Chain (25 分)
    1078 Hashing (25 分)
    1063 Set Similarity (25 分)
  • 原文地址:https://www.cnblogs.com/plxx/p/4451561.html
Copyright © 2020-2023  润新知