ArrayList/LinkedList/Vector
ArrayList
允许包括 null 在内的所有元素、有序、可重复、线程不安全
定义
继承AbstractList,实现List、RandomAccess
1
| public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
|
底层存储
1
| transient Object[] elementData;
|
属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| private static final int DEFAULT_CAPACITY = 10; private static final Object[] EMPTY_ELEMENTDATA = {}; private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; private int size; private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
|
构造方法
1 2 3 4 5 6 7 8 9
| public ArrayList() public ArrayList(int initialCapacity) public ArrayList(Collection<? extends E> c)
|
add
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public boolean add(E e) { ensureCapacityInternal(size + 1); elementData[size++] = e; return true; } public void add(int index, E element) { rangeCheckForAdd(index); ensureCapacityInternal(size + 1); System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = element; size++; }
|
在add一个元素时,才对容量进行了操作,以防止new出一个数组,但是不用,导致空间资源的浪费
扩容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| private void ensureCapacityInternal(int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } ensureExplicitCapacity(minCapacity); } private void ensureExplicitCapacity(int minCapacity) { modCount++; if (minCapacity - elementData.length > 0) grow(minCapacity); } private void grow(int minCapacity) { int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity); }
|
LinkedList
底层基于链表、允许包括null的所有元素、有序、线程不安全
定义
继承AbstractSequentialList,实现List、Deque(即能将LinkedList当作双端队列使用)
1
| public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable
|
属性
1 2 3 4 5 6 7 8
| transient int size = 0; transient Node<E> first; transient Node<E> last;
|
结点定义
1 2 3 4 5 6 7 8 9 10 11 12
| private static class Node<E> { E item; Node<E> next; Node<E> prev; Node(Node<E> prev, E element, Node<E> next) { this.item = element; this.next = next; this.prev = prev; } }
|
构造方法
1 2 3 4
| public LinkedList() public LinkedList(Collection<? extends E> c)
|
常用方法
底层的具体操作都是基于链表的操作,具体查看源码。。
与ArrayList的区别
- ArrayList是实现了基于动态数组的数据结构,而LinkedList是基于链表的数据结构
- 随机访问get和set,ArrayList优于LinkedList / 对于add和remove,LinedList比较占优势
- 当容量不足时,ArrayList需要进行扩容操作,实际上就是创建出一个更大的数组,然后将旧数组中的值拷贝到新数组中。
Vector
与ArrayList一样底层基于动态数组,但线程安全
定义
继承AbstractList,实现List、RandomAccess
1
| public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
|
底层存储
1
| protected Object[] elementData;
|
属性
1 2 3 4 5 6
| protected int elementCount; protected int capacityIncrement; private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
|
构造方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public Vector() public Vector(Collection<? extends E> c) public Vector(int initialCapacity) public Vector(int initialCapacity, int capacityIncrement)
|
扩容
1 2 3 4 5 6 7 8 9 10 11 12 13
| private void grow(int minCapacity) { int oldCapacity = elementData.length; int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity); }
|