Java.util.Stack.remove(int index)方法用于从特定位置或索引中从Stack中删除元素。
用法:
Stack.remove(int index)
参数:此方法接受整数数据类型的强制参数索引,并指定要从堆栈中删除的元素的位置。
返回值:此方法返回刚刚从堆栈中删除的元素。
以下示例程序旨在说明Java.util.Stack.remove(int index)方法:
示例1:
// Java code to illustrate remove() when position of
// element is passed as parameter
import java.util.*;
public class StackDemo {
public static void main(String args[])
{
// Creating an empty Stack
Stack<String> stack = new Stack<String>();
// Use add() method to add elements in the Stack
stack.add("Geeks");
stack.add("for");
stack.add("Geeks");
stack.add("10");
stack.add("20");
// Output the Stack
System.out.println("Stack: " + stack);
// Remove the element using remove()
String rem_ele = stack.remove(4);
// Print the removed element
System.out.println("Removed element: "
+ rem_ele);
// Print the final Stack
System.out.println("Final Stack: "
+ stack);
}
}
输出:
Stack: [Geeks, for, Geeks, 10, 20] Removed element: 20 Final Stack: [Geeks, for, Geeks, 10]
示例2:
// Java code to illustrate remove() when position of
// element is passed as parameter
import java.util.*;
public class StackDemo {
public static void main(String args[])
{
// Creating an empty Stack
Stack<Integer> stack = new Stack<Integer>();
// Use add() method to add elements in the Stack
stack.add(10);
stack.add(20);
stack.add(30);
stack.add(40);
stack.add(50);
// Output the Stack
System.out.println("Stack: " + stack);
// Remove the element using remove()
int rem_ele = stack.remove(0);
// Print the removed element
System.out.println("Removed element: "
+ rem_ele);
// Print the final Stack
System.out.println("Final Stack: "
+ stack);
}
}
输出:
Stack: [10, 20, 30, 40, 50] Removed element: 10 Final Stack: [20, 30, 40, 50]
相关用法
- Java Stack get()用法及代码示例
- Java Stack contains()用法及代码示例
- Java Stack set()用法及代码示例
- Java Stack pop()用法及代码示例
- Java Stack indexOf()用法及代码示例
- Java Stack containsAll()用法及代码示例
- Java Stack subList()用法及代码示例
- Java Stack empty()用法及代码示例
- Java Stack toArray()用法及代码示例
- Java Stack setSize()用法及代码示例
- Java Stack insertElementAt()用法及代码示例
- Java Stack copyInto()用法及代码示例
- Java Stack removeElementAt()用法及代码示例
- Java Stack clear()用法及代码示例
- Java Stack toString()用法及代码示例
注:本文由纯净天空筛选整理自Code_r大神的英文原创作品 Stack remove(int) method in Java with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。