LinkedList vs ArrayList

http://mqbing.iteye.com/blog/76913

LinkedList vs ArrayList

    晚上和同学讨论 LinkedList  和 ArrayList的区别...
   讨论时发现信息太少,于是在网上找了些资料..存一下.


 

今天看到的一个Blog上的内容,我把大致内容摘录下来,作为备忘。

http://javachaos.crazyredpanda.com/?p=99

<o:p> </o:p>

首先看一下LinkedListArrayList的继承关系。

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable

      

public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Queue<E>, Cloneable, Serializable

<o:p> </o:p>

两者都实现List接口,前者实现RandomAccess接口,后者实现Queue接口。

<o:p> </o:p>

ArrayList<o:p></o:p>

ArrayList其实是包装了一个数组 Object[],当实例化一个ArrayList时,一个数组也被实例化,当向ArrayList中添加对象是,数组的大小也相应的改变。这样就带来以下有缺点:

快速随即访问 你可以随即访问每个元素而不用考虑性能问题,通过调用get(i)方法来访问下标为i的数组元素。

向其中添加对象速度慢 当你创建数组是并不能确定其容量,所以当改变这个数组时就必须在内存中做很多事情。

操作其中对象的速度慢 当你要想数组中任意两个元素中间添加对象时,数组需要移动所有后面的对象。

<o:p> </o:p>

LinkedList<o:p></o:p>

LinkedList是通过节点直接彼此连接来实现的。每一个节点都包含前一个节点的引用,后一个节点的引用和节点存储的值。当一个新节点插入时,只需要修改其中保持先后关系的节点的引用即可,当删除记录时也一样。这样就带来以下有缺点:

操作其中对象的速度快 只需要改变连接,新的节点可以在内存中的任何地方

不能随即访问 虽然存在get()方法,但是这个方法是通过遍历接点来定位的所以速度慢。

<o:p> </o:p>

一些结论:<o:p></o:p>

当一些被定义好的数据需要放到与数组对应的List中,ArrayList是很好的选择,因为它可以动态变化,但是不要在整个应用程序用频繁的使用。当你要很方便的操作其中的数据而不用随即访问时LinkList是很好的选择。如果你要频繁随即访问建议使用数组。

另外一个我没有提到的是关于QueueLinkedList的实现使其具有很好的可扩展性,可以方便的在开始和结尾添加删除节点。所以LinkedList很适合用来实现QueueStack,尽管在Java5种已经有了一个Stack的实现。

<o:p> </o:p>

<o:p> </o:p>

以上是原文中的观点,但是在回复中也有人反对:

LinkedList有以下缺陷:

对象分配-每添加一项就分配一个对象

回收垃圾-对象分配的结果

随即访问慢-设计上的原因

添加删除慢-因为首先要找到位置

应该使用LinkedList的情况非常少。大多数的建议使使用LinkedList是错误的。<o:p></o:p>

JDKStack就是用数组来实现的。

在多数时间里你并不是向List中间添加数据,而是向在结尾添加,这样的操作ArrayList表现的很好。

LinkedList在实现Queue时很有用。

<o:p> </o:p>

然后是原文作者提供的一些数据:

addFirst() to array list took 1422<o:p></o:p>

addFirst() to linked list using general methods took 16<o:p></o:p>

addFirst() to linked list using linked list methods took 16<o:p></o:p>

addLast() to array list took 16<o:p></o:p>

addLast() to linked list using general methods took 15<o:p></o:p>

addLast() to linked list using linked list methods took 0<o:p></o:p>

addMiddleTest() to array list took 735<o:p></o:p>

addMiddleTest() to linked list using general methods took 11688<o:p></o:p>

addMiddleTest() to linked list using linked list methods took 8406<o:p></o:p>

removeFirst() to array list took 1422<o:p></o:p>

removeFirst() to linked list using general methods took 0<o:p></o:p>

removeFirst() to linked list using linked list methods took 0<o:p></o:p>

removeLast() to array list took 0<o:p></o:p>

removeLast() to linked list using general methods took 0<o:p></o:p>

removeLast() to linked list using linked list methods took 0<o:p></o:p>

removeMiddle() to array list took 734<o:p></o:p>

removeMiddle() to linked list using general methods took 7594<o:p></o:p>

removeMiddle() to linked list using linked list methods took 7719<o:p></o:p>

fetchFirst() to array list took 0<o:p></o:p>

fetchFirst() to linked list using general methods took 0<o:p></o:p>

fetchFirst() to linked list using linked list methods took 0<o:p></o:p>

fetchLast() to array list took 16<o:p></o:p>

fetchLast() to linked list using general methods took 0<o:p></o:p>

fetchLast() to linked list using linked list methods took 0<o:p></o:p>

fetchMiddle() to array list took 15<o:p></o:p>

fetchMiddle() to linked list using general methods took 9156<o:p></o:p>

fetchMiddle() to linked list using linked list methods took 9234

www.blogjava.net/mstar/archive/2006/01/26/29272.html
分享到:


原文地址:https://www.cnblogs.com/lexus/p/2196771.html