zhengjiong 发表于 2013-2-3 13:39:39

ArrayList对比LinkedList

一般大家都知道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。
 
 4.查找操作indexOf,lastIndexOf,contains等,两者差不多。
5.随机查找指定节点的操作get,ArrayList速度要快于LinkedList.
这里只是理论上分析,事实上也不一定,ArrayList在末尾插入和删除数据的话,速度反而比LinkedList要快。
 
 

<span style="color: #333333; font-family: Arial; font-size: 14px; line-height: 26px;">一.时间复杂度
首先一点关键的是,ArrayList的内部实现是基于基础的对象数组的,因此,它使用get方法访问列表中的任意一个元素时(random access),它的速度要比LinkedList快。LinkedList中的get方法是按照顺序从列表的一端开始检查,直到另外一端。对LinkedList而言,访问列表中的某个指定元素没有更快的方法了。
假设我们有一个很大的列表,它里面的元素已经排好序了,这个列表可能是ArrayList类型的也可能是LinkedList类型的,现在我们对这个列表来进行二分查找(binary search),比较列表是ArrayList和LinkedList时的查询速度,看下面的程序:

<div style="padding-right: 5.4pt; padding-left: 5.4pt; background-color: #e6e6e6; padding-bottom: 4px; width: 659px; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifpackage com.mangocity.test;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.LinkedList;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.List;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.Random;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.ArrayList;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.Arrays;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.util.Collections;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifpublic class TestList {
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    public static final int N=50000;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    public static List values;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    static{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        Integer vals[]=new Integer;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        Random r=new Random();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif        for(int i=0,currval=0;i<N;i++){
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            vals=new Integer(currval);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            currval+=r.nextInt(100)+1;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        values=Arrays.asList(vals);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    static long timeList(List lst){
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        long start=System.currentTimeMillis();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        for(int i=0;i<N;i++)...
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return System.currentTimeMillis()-start;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    public static void main(String args[]){
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        System.out.println("ArrayList消耗时间:"+timeList(new ArrayList(values)));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        System.out.println("LinkedList消耗时间:"+timeList(new LinkedList(values)));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
页: [1]
查看完整版本: ArrayList对比LinkedList