LinkedList 的所有遍历方式

package com.nine;

import java.util.Iterator;
import java.util.LinkedList;

public class Test {
    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        linkedList.add("12");
        linkedList.add("34");
        System.out.println(linkedList);

        Iterator iterator = linkedList.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        System.out.println("--------------");

        for (int i = 0; i < linkedList.size(); i++) {
            System.out.println(linkedList.get(i));
        }

        System.out.println("--------------");

        for (Object object : linkedList) {
            System.out.println(object);
        }

        System.out.println("--------------");

        while (linkedList.size() != 0) {
            System.out.println(linkedList.pollFirst());
        }

        System.out.println("--------------");
        LinkedList linkedList2 = new LinkedList();
        linkedList2.add("12");
        linkedList2.add("34");
        while (linkedList2.size() != 0) {
            System.out.println(linkedList2.pollLast());  // 相当于倒叙输出; 
        }


        System.out.println("--------------");
        LinkedList linkedList3 = new LinkedList();
        linkedList3.add("12");
        linkedList3.add("34");
        while (linkedList3.size() != 0) {
            System.out.println(linkedList3.removeLast());  // 相当于倒叙输出; 
        }
        double   b =  System.currentTimeMillis(); 
        System.out.println("--------------");
        LinkedList linkedList4 = new LinkedList();
        linkedList4.add("12");
        linkedList4.add("34");
        while (linkedList4.size() != 0) {
            System.out.println(linkedList4.removeFirst());  // 相当于正序叙输出; 
        }

        double   a = System.currentTimeMillis(); 
        System.out.println(a-b);   //  1 毫秒; 
    }

}

你可能感兴趣的:(linkedlist,遍历)