Java集合
Java集合是java提供的工具包,包含了常用的数据结构:集合、链表、队列、栈、数组、映射等。Java集合工具包位置是java.util.*
Java集合主要可以划分为4个部分:List列表、Set集合、Map映射、工具类(Iterator迭代器、Enumeration枚举类、Arrays和Collections)、。
Vector
数组实现,默认初始大小为10
同步实现,synchronized修饰方法
3个成员变量:elementData
, elementCount
, capacityIncrement
-
elementData
是"Object[]类型的数组",它保存了添加到Vector中的元素。elementData
是个动态数组,如果初始化Vector
时,没指定动态数组的>大小,则使用默认大小10。随着Vector
中元素的增加,Vector
的容量也会动态增长,capacityIncrement
是与容量增长相关的增长系数,具体的增长方式,请参考源码分析中的ensureCapacity()
函数。 -
elementCount
是动态数组的实际大小。 -
capacityIncrement
是动态数组的增长系数。如果在创建Vector
时,指定了capacityIncrement
的大小;则,每次当Vector中动态数组容量增加时>,增加的大小都是capacityIncrement
。
//Vector共有4个构造函数
// 默认构造函数
Vector()
// capacity是Vector的默认容量大小。当由于增加数据导致容量增加时,每次容量会增加一倍。
Vector(int capacity)
// capacity是Vector的默认容量大小,capacityIncrement是每次Vector容量增加时的增量值。
Vector(int capacity, int capacityIncrement)
// 创建一个包含collection的Vector
Vector(Collection<? extends E> collection)
/**
* The amount by which the capacity of the vector is automatically
* incremented when its size becomes greater than its capacity. If
* the capacity increment is less than or equal to zero, the capacity
* of the vector is doubled each time it needs to grow.
*/
//默认为为0
protected int capacityIncrement;
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private void grow(int minCapacity) {
// overflow-conscious code
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);
}