集合框架之List LinkedList

LinkedList也是List接口的实现类,底层数据结构为链表。

常用方法和遍历方法参照List。

LinkedList是线程不安全的。

 

 

LinkedList除了实现List接口,还是实现了栈接口。

 1 public class Test01 {
 2     public static void main(String[] args) {
 3         LinkedList list = new LinkedList();
 4         list.push("apple");
 5         list.push("banana");
 6         list.push("coco");
 7         
 8         
 9         System.out.println(list.pop());
10         System.out.println(list.pop());
11         System.out.println(list.pop());
12         
13         // java.util.NoSuchElementException
14         System.out.println(list.pop());
15     }
16 }

 

实现了队列(queue)接口

 

 

 

add/remove/element() 可能会出现NoSuchElementException异常

 1 public static void main(String[] args) {
 2         
 3         LinkedList queue = new LinkedList();
 4         // 入队
 5         /**
 6          * 队列头                          队列尾
 7          *<-----          <-----
 8          * [apple, banana, coco]
 9          */
10         queue.add("apple");
11         queue.add("banana");
12         queue.add("coco");
13         System.out.println(queue);
14         
15         // 出队
16         System.out.println(queue.remove());
17         System.out.println(queue.remove());
18         System.out.println(queue.remove());        
19         System.out.println(queue);
20         
21         // java.util.NoSuchElementException
22         System.out.println(queue.remove());
23         
24         
25         // 获取表头元素
26         System.out.println(queue.element());
27     }

 

offer/poll/peek 可能会返回特殊值(null)

 1 public static void main(String[] args) {
 2         
 3         LinkedList queue = new LinkedList();
 4         // 入队
 5         /**
 6          * 队列头                          队列尾
 7          *<-----          <-----
 8          * [apple, banana, coco]
 9          */
10         queue.offer("apple");
11         queue.offer("banana");
12         queue.offer("coco");
13         
14         // 出队列
15         //System.out.println(queue.poll());
16         //System.out.println(queue.poll());
17         //System.out.println(queue.poll());
18         System.out.println(queue);
19 
20         //System.out.println(queue.poll());
21         
22         // 获取表头元素
23         System.out.println(queue.peek());
24     
25     }

 

双向队列(Deque)接口

 

/**
 * 以双向队列形式操作LinkedList
 */
public class Test04 {
    public static void main(String[] args) {
        
        LinkedList queue = new LinkedList();
        // 入队
        /**
         *<-----          <-----
         * [apple, banana, coco]
         * ---->          ----->
         */
        
        queue.addFirst("apple");
        queue.addFirst("banana");
        queue.addFirst("coco");
        System.out.println(queue);
        
        System.out.println(queue.removeLast());
        System.out.println(queue.removeFirst());
        System.out.println(queue.removeFirst());
        System.out.println(queue);
        
        // 获取头元素
        System.out.println(queue.getFirst());
    
    }
}

 

posted @ 2019-05-05 21:10  luojack  阅读(143)  评论(0编辑  收藏  举报