java基礎(集合List-ArrayList、LinkedList、Vector的區別)

ArrayListLinkedListVector的區別

ArrayList底層實際上是採用數組實現的(並且該數組的類型的Object類型的)

②如果jdk6,採用Array.copyOf()方法來生成一個新的數組,如果是jdk5,採用的是System.arraycopy()方法(當添加的數據量大於數組的長度時候)

List list = newArrayList()時,底層會生成一個長度爲10的數組來存放對象

 

ArrayListVector底層都是採用數組實現的

⑤對於ArrayList和LinkeList,方法都不是同步的,對於Vector,大部分public方法都是同步的,

LinkedList採用雙向循環鏈表

 

⑦對於ArrayList,查詢速度很快,增加和刪除(非最後一個節點)操作非常慢(本質上由數組的特性決定的)

⑧對於LinkedList,查詢速度非常慢,增加和刪除操作非常快(本質上是由雙向循環鏈表決定的)




1、ArrayList的默認構造方法代碼

[java] view plain copy
  1. private transient Object elementData[];  
  2.   
  3. public ArrayList(int i) {  
  4.         if (i < 0) {  
  5.             throw new IllegalArgumentException((new StringBuilder())  
  6.                     .append("Illegal Capacity: ").append(i).toString());  
  7.         } else {  
  8.             elementData = new Object[i];  
  9.             return;  
  10.         }  
  11.     }  
  12.   
  13.     public ArrayList() {  
  14.         this(10);  
  15.     }  

2、LinkList默認構造方法 

[java] view plain copy
  1. public LinkedList() {  
  2.         header = new Entry(nullnullnull);  
  3.         size = 0;  
  4.         header.next = header.previous = header;  
  5.     }  
  6.   
  7. private static class Entry {  
  8.   
  9.         Object element;  
  10.         Entry next;  
  11.         Entry previous;  
  12.   
  13.         Entry(Object obj, Entry entry1, Entry entry2) {  
  14.             element = obj;  
  15.             next = entry1;  
  16.             previous = entry2;  
  17.         }  
  18.     }  
  19.   
  20.   
  21. private transient Entry header;  
  22.     private transient int size;  
3、Vector默認構造方法

[java] view plain copy
  1. public Vector(int i, int j) {  
  2.         if (i < 0) {  
  3.             throw new IllegalArgumentException((new StringBuilder())  
  4.                     .append("Illegal Capacity: ").append(i).toString());  
  5.         } else {  
  6.             elementData = new Object[i];  
  7.             capacityIncrement = j;  
  8.             return;  
  9.         }  
  10.     }  
  11.   
  12.     public Vector(int i) {  
  13.         this(i, 0);  
  14.     }  
  15.   
  16.     public Vector() {  
  17.         this(10);  
  18.     }  
  19.   
  20. protected Object elementData[];  
  21.   
  22. protected int capacityIncrement;  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章