我有个Vector类的疑问,帮帮忙,应该是很简单的问题
Vectorvc=newVector<String>(1);我这样定义了一个vc,为什么我不断地vc.add("fff")重复了10遍都不会报错?vc容量不是只有1吗?...
Vector vc = new Vector<String>(1);
我这样定义了一个vc,为什么我不断地vc.add("fff")重复了10遍都不会报错?vc容量不是只有1吗? 展开
我这样定义了一个vc,为什么我不断地vc.add("fff")重复了10遍都不会报错?vc容量不是只有1吗? 展开
1个回答
展开全部
Vector默认大小为10,现在你指定是1,不过它会动态增加容量。看一下Vector的函数定义就知道了。
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.
* @exception 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 <tt>10</tt> and its standard capacity increment is
* zero.
*/
public Vector() {
this(10);
}
public Vector(Collection<? extends E> c) {
elementCount = c.size();
// 10% for growth
elementData = new Object[
(int)Math.min((elementCount*110L)/100,Integer.MAX_VALUE)];
c.toArray(elementData);
}
private void ensureCapacityHelper(int minCapacity) {
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object[] oldData = elementData;
int newCapacity = (capacityIncrement > 0) ?
(oldCapacity + capacityIncrement) : (oldCapacity * 2);
if (newCapacity < minCapacity) {
newCapacity = minCapacity;
}
elementData = new Object[newCapacity];
System.arraycopy(oldData, 0, elementData, 0, elementCount);
}
}
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.
* @exception 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 <tt>10</tt> and its standard capacity increment is
* zero.
*/
public Vector() {
this(10);
}
public Vector(Collection<? extends E> c) {
elementCount = c.size();
// 10% for growth
elementData = new Object[
(int)Math.min((elementCount*110L)/100,Integer.MAX_VALUE)];
c.toArray(elementData);
}
private void ensureCapacityHelper(int minCapacity) {
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object[] oldData = elementData;
int newCapacity = (capacityIncrement > 0) ?
(oldCapacity + capacityIncrement) : (oldCapacity * 2);
if (newCapacity < minCapacity) {
newCapacity = minCapacity;
}
elementData = new Object[newCapacity];
System.arraycopy(oldData, 0, elementData, 0, elementCount);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询