int capacity()
描述 (Description)
capacity()方法用于返回向量的当前容量。 Capacity是保存在vactor中的数组的长度。
声明 (Declaration)
以下是java.util.Vector.capacity()方法的声明
public int capacity()
参数 (Parameters)
此方法不接受任何参数作为输入。
返回值 (Return Value)
该方法将向量的current capacity作为integer返回。此容量表示length of its internal data array的length of its internal data array ,保存在此向量的字段elementData中。
异常 (Exception)
NA
例子 (Example)
以下示例显示了java.util.Vector.capacity()方法的用法。
package com.iowiki;
import java.util.Vector;
public class VectorDemo {
public static void main(String[] args) {
// create an empty Vector vec
Vector<Integer> vec = new Vector<Integer>();
// use add() method to add elements in the vector
vec.add(14);
vec.add(13);
vec.add(12);
vec.add(11);
vec.add(4);
vec.add(3);
vec.add(2);
vec.add(1);
// let us print the capacity of the vector
System.out.println("Capacity of the vector is :"+vec.capacity());
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
Capacity of the vector is :10