Java.util.Stack 类

java.util.Stack.capacity() 方法返回堆栈的当前容量。

语法

public int capacity()


Parameters
No parameter is required.

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

Exception
NA.

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





import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //创建一个栈
    Stack<Integer> MyStack = new Stack<Integer>();

    //填充堆栈
    MyStack.push(10);
    MyStack.push(20);
    MyStack.push(30);
    MyStack.push(40);
    MyStack.push(50);

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

上述代码的输出将是:

Size of MyStack: 5
Capacity of MyStack: 10