ArrayList,LinkedList,Vector,Stack之间的区别

一,线程安全性

Vector、Stack:线程安全

ArrayList、LinkedList:非线程安全

 

二,实现方式

LinkedList:双向链表

ArrayList,Vector,Stack:数组

 

三,容量扩展方面

由于ArrayList和Vector(Stack继承自Vector,只在Vector的基础上添加了几个Stack相关的方法,故之后不再对Stack做特别的说明)使用数组实现,当数组长度不够时,其内部会创建一个更大的数组,然后将原数组中的数据拷贝至新数组中

[java]  view plain  copy
  1. //ArrayList  
  2. public boolean add(E e) {  
  3.     ensureCapacity(size + 1);  
  4.     elementData[size++] = e;  
  5.     return true;  
  6. }  
  7.   
  8. public void ensureCapacity(int minCapacity) {  
  9.     modCount++;  
  10.     int oldCapacity = elementData.length;  
  11.     if (minCapacity > oldCapacity) {  
  12.         Object oldData[] = elementData;  
  13.         int newCapacity = (oldCapacity * 3)/2 + 1;  
  14.         //如果这次扩展不能满足要求,那就直接用minCapacity  
  15.         if (newCapacity < minCapacity)  
  16.             newCapacity = minCapacity;  
  17.         // minCapacity is usually close to size, so this is a win:  
  18.         elementData = Arrays.copyOf(elementData, newCapacity);  
  19.     }  
  20. }  

如需扩展,则每次至少扩展至(原长度*3)/2 + 1

[java]  view plain  copy
  1. //Vector  
  2. public synchronized void addElement(E obj) {  
  3.     modCount++;  
  4.     ensureCapacityHelper(elementCount + 1);  
  5.     elementData[elementCount++] = obj;  
  6. }  
  7.   
  8. private void ensureCapacityHelper(int minCapacity) {  
  9.     int oldCapacity = elementData.length;  
  10.     if (minCapacity > oldCapacity) {  
  11.         Object[] oldData = elementData;  
  12.         int newCapacity = (capacityIncrement > 0) ?  
  13.             (oldCapacity + capacityIncrement) : (oldCapacity * 2);  
  14.         //如果这次扩展不能满足要求,那就直接用minCapacity  
  15.         if (newCapacity < minCapacity) {  
  16.             newCapacity = minCapacity;  
  17.         }  
  18.         elementData = Arrays.copyOf(elementData, newCapacity);  
  19.     }  
  20. }  

如果在创建Vector时不指定capacityIncrement(自动扩展长度)的值,如需扩展,则每次至少扩展至原长度的2倍

 

四,效率方面

这里仅仅比较ArrayList和LinkedList之间的效率差异

1,查询

ArrayList直接通过下标进行定位

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //ArrayList  
  2. public E get(int index) {  
  3.     RangeCheck(index);//检查下标是否超过数组长度  
  4.     return (E) elementData[index];  
  5. }  

LinkedList则需要进行遍历,平均遍历次数应为n/4

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //LinkedList  
  2. public E get(int index) {  
  3.     return entry(index).element;  
  4. }  
  5.       
  6. private Entry<E> entry(int index) {  
  7.     if (index < 0 || index >= size)  
  8.         throw new IndexOutOfBoundsException("Index: "+index+  
  9.                 ", Size: "+size);  
  10.     Entry<E> e = header;  
  11.     //size >>1 相当于size/2  
  12.     //由于LinkedList由双向链表实现,故从离得较近的一端开始遍历更快  
  13.     if (index < (size >> 1)) {  
  14.         for (int i = 0; i <= index; i++)  
  15.             e = e.next;  
  16.     } else {  
  17.         for (int i = size; i > index; i--)  
  18.             e = e.previous;  
  19.     }  
  20.     return e;  
  21. }  

对于指定位置查询,由于可以通过下标直接进行定位,ArrayList的速度远快于LinkedList

但是如果都为首尾位置的查询,情况会大为不同,因为LinkedList也是可以直接定位到首尾位置的

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //LinkedList  
  2. public E getFirst() {  
  3.     if (size==0)  
  4.         throw new NoSuchElementException();  
  5.     return header.next.element;  
  6. }  
  7.   
  8. public E getLast()  {  
  9.     if (size==0)  
  10.         throw new NoSuchElementException();  
  11.     return header.previous.element;  
  12. }  

此时ArrayList和LinkedList的效率相同

2,插入

对于ArrayList,指定位置插入有可能首先需要对数组容量进行扩展,之后还有可能导致数组中的数据需要顺次移动(代码中通过数组拷贝实现,避免了数据一个一个的移动),极端情况下插入一个数据将进行两次数组拷贝

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //ArrayList  
  2. public void add(int index, E element) {  
  3.     if (index > size || index < 0)  
  4.         throw new IndexOutOfBoundsException(  
  5.                 "Index: "+index+", Size: "+size);  
  6.     ensureCapacity(size+1);  //如必要,将对数组容量进行扩展  
  7.     System.arraycopy(elementData, index, elementData, index + 1,  
  8.             size - index);  
  9.     elementData[index] = element;  
  10.     size++;  
  11. }  

如果不指定插入位置,则插入至数组末端,此时只需考虑可能的数组容量扩展对性能带来的影响

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //ArrayList  
  2. public boolean add(E e) {  
  3.     ensureCapacity(size + 1);   
  4.     elementData[size++] = e;  
  5.     return true;  
  6. }  

由于LinkedList是由链表实现的,并没有指定位置插入的方法,即便如此,一切也显得如此美好

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //LinkedList  
  2. public boolean add(E e) {  
  3.     addBefore(e, header);  
  4.         return true;  
  5. }  
  6.       
  7. private Entry<E> addBefore(E e, Entry<E> entry) {  
  8.     Entry<E> newEntry = new Entry<E>(e, entry, entry.previous);  
  9.     newEntry.previous.next = newEntry;  
  10.     newEntry.next.previous = newEntry;  
  11.     size++;  
  12.     modCount++;  
  13.     return newEntry;  
  14. }  

当然了,LinkedList可以直接将数据插入至首尾

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //LinkedList  
  2. public void addFirst(E e) {  
  3.     addBefore(e, header.next);  
  4. }  
  5.   
  6. public void addLast(E e) {  
  7.     addBefore(e, header);  
  8. }  

总体来说,LinkedList效率高于ArrayList,即使在末尾插入,ArrayList也需要考虑可能的容量扩展对性能带来的影响

3,修改

和查询属于同一种情况

4,删除

指定位置的删除和插入属于同一种情况

除了删除指定位置数据,ArrayList和LinkedList都包含一个clear()方法用来清除所有数据

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //ArrayList  
  2. public void clear() {  
  3.     modCount++;  
  4.   
  5.     // Let gc do its work  
  6.     for (int i = 0; i < size; i++)  
  7.         elementData[i] = null;  
  8.     size = 0;  
  9. }  
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //LinkedList  
  2. public void clear() {  
  3.     Entry<E> e = header.next;  
  4.     while (e != header) {  
  5.         Entry<E> next = e.next;  
  6.         e.next = e.previous = null;  
  7.         e.element = null;  
  8.         e = next;  
  9.     }  
  10.     header.next = header.previous = header;  
  11.     size = 0;  
  12.     modCount++;  
  13. }  
由于都需要进行遍历,故效率相同
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值