List学习笔记

List:有序列表,允许存放重复的元素; 

实现类比较: 
ArrayList:数组实现,查询快,增删慢,线程不安全,轻量级;
LinkedList:链表实现,增删快,查询慢 
Vector:数组实现,线程安全,重量级

ArrayList重要源码讲解:

初始容量为10

1 private static final int DEFAULT_CAPACITY = 10;

定义一个空的数组实例

1 private static final Object[] EMPTY_ELEMENTDATA = {};

定义一个可缓冲的数组变量

1 private transient Object[] elementData;

构造方法:

1 public ArrayList() {
2         super();
3         this.elementData = EMPTY_ELEMENTDATA;
4   }

 

ArrayList是以0.5倍的递增速率给自己扩容量的

 1 private void grow(int minCapacity) {
 2         // overflow-conscious code
 3         int oldCapacity = elementData.length;
 4         int newCapacity = oldCapacity + (oldCapacity >> 1);
 5         if (newCapacity - minCapacity < 0)
 6             newCapacity = minCapacity;
 7         if (newCapacity - MAX_ARRAY_SIZE > 0)
 8             newCapacity = hugeCapacity(minCapacity);
 9         // minCapacity is usually close to size, so this is a win:
10         elementData = Arrays.copyOf(elementData, newCapacity);
11     }

ArrayList查询快的原因如下:

1  public E get(int index) {
2         rangeCheck(index);
3 
4         return elementData(index);
5     }
6 
7 E elementData(int index) {
8         return (E) elementData[index];
9     }

 

LinkedList重要源码讲解:

定义前尾节点:

1  transient Node<E> first;
2 
3  transient Node<E> last;

LinkedList增删快的原因如下:

 1 public boolean add(E e) {
 2         linkLast(e);
 3         return true;
 4     }
 5 
 6 void linkLast(E e) {
 7         final Node<E> l = last;
 8         final Node<E> newNode = new Node<>(l, e, null);
 9         last = newNode;
10         if (l == null)
11             first = newNode;
12         else
13             l.next = newNode;
14         size++;
15         modCount++;
16     }
 1  public E remove(int index) {
 2         checkElementIndex(index);
 3         return unlink(node(index));
 4     }
 5 
 6 E unlink(Node<E> x) {
 7         // assert x != null;
 8         final E element = x.item;
 9         final Node<E> next = x.next;
10         final Node<E> prev = x.prev;
11 
12         if (prev == null) {
13             first = next;
14         } else {
15             prev.next = next;
16             x.prev = null;
17         }
18 
19         if (next == null) {
20             last = prev;
21         } else {
22             next.prev = prev;
23             x.next = null;
24         }
25 
26         x.item = null;
27         size--;
28         modCount++;
29         return element;
30     }

 LinkedList查询慢的原因如下:

 1 public E get(int index) {
 2         checkElementIndex(index);
 3         return node(index).item;
 4     }
 5 Node<E> node(int index) {
 6         // assert isElementIndex(index);
 7 
 8         if (index < (size >> 1)) {
 9             Node<E> x = first;
10             for (int i = 0; i < index; i++)
11                 x = x.next;
12             return x;
13         } else {
14             Node<E> x = last;
15             for (int i = size - 1; i > index; i--)
16                 x = x.prev;
17             return x;
18         }
19     }

Vector重要源码讲解:

类似ArrayList的几个变量

1 protected Object[] elementData;
2 
3 protected int elementCount;
4 //当capacityIncrement<=0,以1倍的扩容
5 protected int capacityIncrement;

Vector初始容量为10

public Vector() {
        this(10);
    }

Vector是线程安全的:

 1 public synchronized E get(int index) {
 2         if (index >= elementCount)
 3             throw new ArrayIndexOutOfBoundsException(index);
 4 
 5         return elementData(index);
 6     }
 7 
 8 E elementData(int index) {
 9         return (E) elementData[index];
10     }
1 public synchronized boolean add(E e) {
2         modCount++;
3         ensureCapacityHelper(elementCount + 1);
4         elementData[elementCount++] = e;
5         return true;
6     }

 

posted @ 2016-03-15 14:32  你又不是个演员  阅读(172)  评论(0)    收藏  举报