- 数组中实际元素数量
*/
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.
-
@serial
-
每次扩容的大小,如果为 0,每次扩容为原来的两倍
*/
protected int capacityIncrement;
/**
-
Constructs an empty vector with the specified initial capacity and
-
capacity increment.
-
@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.
-
@param initialCapacity the initial capacity of the vector
-
@throws IllegalArgumentException if the specified initial capacity
-
is negative
-
初始化容器并指定初始容量,扩容大小默认为0(即每次扩容为原来的两倍)
*/
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,扩容大小默认为0(即每次扩容为原来的两倍)
*/
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.
-
@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);
}
3:增删查改
增加和删除操作才算作结构的改变,才会记录修改次数。修改时不记录修改次数。
3.1:增加元素
/**
-
Appends the specified element to the end of this Vector.
-
@param e element to be appended to this Vector
-
@return {@code true} (as specified by {@link Collection#add})
-
@since 1.2
-
插入元素到数组的末尾
*/
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
/**
-
Adds the specified component to the end of this vector,
-
increasing its size by one. The capacity of this vector is
-
increased if its size becomes greater than its capacity.
-
This method is identical in functionality to the
-
{@link #add(Object) add(E)}
-
method (which is part of the {@link List} interface).
-
@param obj the component to be added
-
与add(E e)方法相同,但是没有返回值
*/
public synchronized void addElement(E obj) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = obj;
}
/**
-
This implements the unsynchronized semantics of ensureCapacity.
-
Synchronized methods in this class can internally call this
-
method for ensuring capacity without incurring the cost of an
-
extra synchronization.
-
@see #ensureCapacity(int)
-
超出容量即进行扩增
*/
private void ensureCapacityHelper(int minCapacity) {
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
/**
-
The maximum size of array to allocate.
-
Some VMs reserve some header words in an array.
-
Attempts to allocate larger arrays may result in
-
OutOfMemoryError: Requested array size exceeds VM limit
-
允许数组最大容量值
*/
最后
本人也收藏了一份Java面试核心知识点来应付面试,借着这次机会可以送给我的读者朋友们
目录:
Java面试核心知识点
一共有30个专题,足够读者朋友们应付面试啦,也节省朋友们去到处搜刮资料自己整理的时间!
Java面试核心知识点
已经有读者朋友靠着这一份Java面试知识点指导拿到不错的offer了
面试核心知识点来应付面试,借着这次机会可以送给我的读者朋友们
目录:
[外链图片转存中…(img-ZOxZbB42-1714319185850)]
Java面试核心知识点
一共有30个专题,足够读者朋友们应付面试啦,也节省朋友们去到处搜刮资料自己整理的时间!
[外链图片转存中…(img-VSan2zUN-1714319185851)]
Java面试核心知识点
已经有读者朋友靠着这一份Java面试知识点指导拿到不错的offer了
[外链图片转存中…(img-UW1oIA5H-1714319185851)]