@kiraSally
2018-03-12T18:42:01.000000Z
字数 4353
阅读 1981
JAVA
COLLECTIONS
源码
1.7版本
- 笔者个人博客 kiraSally的掘金个人博客述 感谢支持
ArrayList
,线程安全ArrayList
,线程安全推荐 CopyOnWriteList
ArrayList
,几乎所有public方法全部由 syncronized
修饰,因此线程安全ArrayList
代码基本一致(后者是前者的非同步代替类),因此仅仅只解析部分有差异的方法
public class Vector<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
- 继承
AbstractList
,实现了List
,它是一个数组队列,提供了相关的添加、删除、修改、遍历等功能- 实现
RandmoAccess
接口,实现快速随机访问:通过元素的序号快速获取元素对象- 实现
Cloneable
接口,重写clone()
,能被克隆(浅拷贝)- 实现
java.io.Serializable
接口,支持序列化
/**
* The array buffer into which the components of the vector are stored.
* The capacity of the vector is the length of this array buffer,
* and is at least large enough to contain all the vector's elements.
* 底层数据结构为动态数组
* <p>Any array elements following the last element in the Vector are null.
* @serial
*/
protected Object[] elementData;
/**
* The number of valid components in this {@code Vector} object.
* Components {@code elementData[0]} through
* {@code elementData[elementCount-1]} are the actual items.
* 数组长度 == 数组的length == ArrayList的size()
* @serial
*/
protected int elementCount;
/**
* 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.
* 当容量不够时,每次Vector容量增加时的增量值;若该值<=0,则默认自动扩容两倍
* @serial
*/
protected int capacityIncrement;
/**
* Constructs an empty vector with the specified initial capacity and
* capacity increment.
* 创建一个指定容量和容量增量值的Vector
* @param initialCapacity the initial capacity of the vector
* @param capacityIncrement the amount by which the capacity is
* increased when the vector overflows
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
/**
* Constructs an empty vector with the specified initial capacity and
* with its capacity increment equal to zero.
* 创建一个指定容量的Vector,容量不够时自动扩容两倍
* @param initialCapacity the initial capacity of the vector
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
/**
* Constructs an empty vector so that its internal data array has size {@code 10}
* and its standard capacity increment is zero.
* 默认构造器,初始容量为10(跟ArrayList一样),容量不够时自动扩容两倍
*/
public Vector() {
this(10);
}
/**
* Constructs a vector containing the elements of the specified collection,
* in the order they are returned by the collection's iterator.
* 创建一个包含collection的Vector
* @param c the collection whose elements are to be placed into this vector
* @throws NullPointerException if the specified collection is null
* @since 1.2
*/
public Vector(Collection<? extends E> c) {
elementData = c.toArray();
elementCount = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
}
/**
* 数组自动扩容
* 区别于ArrayList 的 (int)Math.floor(oldCapacity*1.5)
* Vector的扩容方案为:
* 当增值量 > 0 时,新容量=原容量+增值量
* 当增值量 <=0 时,新容量=原容量*2(但采用的是加法而非更高效的位运算)
*/
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);
}
/**
* Save the state of the {@code Vector} instance to a stream (that is, serialize it).
* This method performs synchronization to ensure the consistency of the serialized data.
* 比较有意思的是虽然Vector支持序列化,但并没有像ArrayList支持提供 writeObject() 和 readObject()
* 来分别支持 序列化和反序列化 的方法级别的直接支持, 唯一的 序列化方法竟然只是private级别。
* 因此想实现Vector的序列化和反序列化功能必须通过手动通过 ObjectOutputStream实现
*/
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
final java.io.ObjectOutputStream.PutField fields = s.putFields();
final Object[] data;
//该方法使用同步代码块(synchronized的推荐使用方法) -- 具体请期待笔者的`并发包系列`
synchronized (this) {
fields.put("capacityIncrement", capacityIncrement);
fields.put("elementCount", elementCount);
data = elementData.clone();
}
fields.put("elementData", data);
s.writeFields();
}
集合番@Vector一文通(1.7版) 由 黄志鹏kira 创作,采用 知识共享 署名-非商业性使用 4.0 国际 许可协议 进行许可。
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。