Java.util.Vector 类

java.util.Vector.capacity() 方法返回向量的当前容量。

语法

public int capacity()


Parameters
No parameter is required.

Return Value
Returns the current capacity of the vector (the length of its internal data array, kept in the field elementData of this vector).

Exception
NA.


Example:
In the example below, the java.util.Vector.capacity() method is used to find out the current capacity of the vector.





import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //创建一个向量
    Vector<Integer> MyVector = new Vector<Integer>(10);

    //填充向量
    MyVector.add(10);
    MyVector.add(20);
    MyVector.add(30);
    MyVector.add(40);
    MyVector.add(50);

    System.out.println("Size of MyVector: " + MyVector.size()); 
    System.out.println("Capacity of MyVector: " + MyVector.capacity()); 
  }
}

上述代码的输出将是:

Size of MyVector: 5
Capacity of MyVector: 10