java基础(集合List-ArrayList、LinkedList、Vector的区别)

来源:互联网 发布:淘宝网禁止黄赌毒 编辑:程序博客网 时间:2024/05/17 09:40

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;  
阅读全文
0 0