ArrayList和LinkedList的區別__java

來源:互聯網
上載者:User
一般大家都知道ArrayList和LinkedList的大致區別:
1.ArrayList是實現了基於動態數組的資料結構,LinkedList基於鏈表的資料結構。
2.對於隨機訪問get和set,ArrayList覺得優於LinkedList,因為LinkedList要移動指標。
3.對於新增和刪除操作add和remove,LinedList比較佔優勢,因為ArrayList要移動資料。
    這一點要看實際情況的。 若只對單條資料插入或刪除,ArrayList的速度反而優於LinkedList。但若是批量隨機的插入刪除資料,LinkedList的速度大大優於ArrayList. 因為ArrayList每插入一條資料,要移動插入點及之後的所有資料。  這一點我做了實驗。在分別有200000條“記錄”的ArrayList和LinkedList的首位插入20000條資料,LinkedList耗時約是ArrayList的20分之1。 for( int m=0;m<20000;m++){
        linkedlist.add(m, null);       // 當在200000條資料之前插入20000條資料時,LinkedList只用了1125多ms.這就是LinkedList的優勢所在
        }
         long time4 =  new Dte().getTime();
        System.out.print("batch linkedlist add:");
        System.out.println(time4 - time3);
         for( int n=0;n<20000;n++){
        arraylist.add(n,  null);   // 當在200000條資料之前插入20000條資料時,ArrayList用了18375多ms.時間花費是arraylist的近20倍(視測試時機器效能)
        }
         long time5 =  new Date().getTime();
        System.out.print("batch arraylist add:");
        System.out.println(time5 - time4);


4.尋找操作indexOf,lastIndexOf,contains等,兩者差不多。
5.隨機尋找指定節點的操作get,ArrayList速度要快於LinkedList.
這裡只是理論上分析,事實上也不一定,ArrayList在末尾插入和刪除資料的話,速度反而比LinkedList要快。我做過一個插入和刪除200000條資料的實驗。          long time1 =  new Date().getTime();
        String s1 = (String) linkedlist.get(100000); //   總記錄200000,linkedlist載入第100000條資料耗時15~32ms不等
         long time2 =  new Date().getTime();
        System.out.println(time2 - time1);
        String s2 = (String) arraylist.get(100000); //   總記錄200000,linkedlist載入第100000條資料耗時0ms
         long time3 =  new Date().getTime();
        System.out.println(time3 - time2);

       /* 分別insert200000條資料到linkedlist和arraylist
    *由於是在末尾插入資料,arraylist的速度比linkedlist的速度反而要快 
     */
     public  static  void insertList(LinkedList linklist, ArrayList arraylist) {
         long time1 =  new Date().getTime();
        System.out.println(time1);
         for ( int i = 0; i < 200000; i++) {
            linklist.add(i, "linklist" + i);
        }
         long time2 =  new Date().getTime();
        System.out.println(time2 - time1);
         for ( int j = 0; j < 200000; j++) {
            arraylist.add(j, "arraylist" + j);
        }
         long time3 =  new Date().getTime();
        System.out.println(time3 - time2);
    }


/* delete linkedlist和arraylist中的200000條資料
    *由於是在末尾刪除資料,arraylist的速度比linkedlist的速度反而要快 
     */
     public  static  void deleteList(LinkedList linklist, ArrayList arraylist) {
         long time1 =  new Date().getTime();
        System.out.println(time1);
         for ( int i = 199999; i >= 0; i--) {
            linklist.remove(i);
        }
         long time2 =  new Date().getTime();
        System.out.println(time2 - time1);
         for ( int j = 199999; j >= 0; j--) {
            arraylist.remove(j);
        }
         long time3 =  new Date().getTime();
        System.out.println(time3 - time2);
    }

     public  static  void main(String args[]) {
        LinkedList linkedlist =  new LinkedList();
        ArrayList arraylist =  new ArrayList();
        insertList(linkedlist, arraylist);

                                         // 以下代碼省略

插入:
LinkedList 578ms
ArrayList 437ms
刪除:
LinkedList 31ms
ArrayList 16ms

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.