admin管理员组文章数量:1611522
堆栈类的capacity()方法用于获取此堆栈的容量。那就是该堆栈容器中存在的元素数。
用法:
public int capacity()
参数:该函数接受参数E obj,它是要在堆栈末尾添加的对象。
返回值:该方法返回整数值,即堆栈的容量
以下示例程序旨在说明Java.util.Stack.capacity()方法:
示例1:
// Java code to illustrate boolean capacity()
import java.util.*;
import java.util.ArrayList;
public class GFG {
public static void main(String args[])
{
// Creating an empty Stack
Stack stack = new Stack();
// Use add() method to add elements in the Stack
stack.add("Geeks");
stack.add("for");
stack.add("Geeks");
stack.add("10");
stack.add("20");
// Displaying the Stack
System.out.println("The Stack is: " + stack);
// checking capacity
System.out.println("Capacity: "
+ stack.capacity());
}
}
输出:
The Stack is: [Geeks, for, Geeks, 10, 20]
Capacity: 10
示例2:
// Java code to illustrate
// boolean add(Object element)
import java.util.*;
public class StackDemo {
public static void main(String args[])
{
// Creating an empty Stack
Stack stack
= new Stack();
// Use add() method
// to add elements in the Stack
stack.add(10);
stack.add(20);
stack.add(30);
stack.add(40);
stack.add(50);
// Displaying the Stack
System.out.println("The Stack is: " + stack);
// checking capacity
System.out.println("Capacity: "
+ stack.capacity());
}
}
输出:
The Stack is: [10, 20, 30, 40, 50]
Capacity: 10
本文标签: 示例代码capacityJavaJavaCapacity
版权声明:本文标题:java capacity_Java Stack capacity()用法及代码示例 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1728619450a1166158.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论