Java.util.Stack.remove(Object o)方法用於從堆棧中刪除任何特定元素。
用法:
Stack.remove(Object o)
參數:此方法接受對象類型為Stack的強製性參數,並指定要從Stack中刪除的元素。
返回值:如果找到指定元素並將其從堆棧中刪除,則返回True,否則返回False。
以下示例程序旨在說明Java.util.Stack.remove(Object o)方法:
示例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()
boolean res = stack.remove("20");
// Print the removed element
System.out.println("Was 20 removed: "
+ res);
// Print the final Stack
System.out.println("Final Stack: "
+ stack);
}
}
輸出:
Stack: [Geeks, for, Geeks, 10, 20] Was 20 removed: true 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()
boolean res = stack.remove("100");
// Print the removed element
System.out.println("Was 100 removed: "
+ res);
// Print the final Stack
System.out.println("Final Stack: "
+ stack);
}
}
輸出:
Stack: [10, 20, 30, 40, 50] Was 100 removed: false Final Stack: [10, 20, 30, 40, 50]
相關用法
- Java Stack pop()用法及代碼示例
- Java Stack set()用法及代碼示例
- Java Stack contains()用法及代碼示例
- Java Stack get()用法及代碼示例
- Java Stack toArray(T[])用法及代碼示例
- Java Stack setElementAt()用法及代碼示例
- Java Stack removeRange()用法及代碼示例
- Java Stack isEmpty()用法及代碼示例
- Java Stack empty()用法及代碼示例
- Java Stack insertElementAt()用法及代碼示例
- Java Stack elementAt()用法及代碼示例
- Java Stack toString()用法及代碼示例
- Java Stack firstElement()用法及代碼示例
- Java Stack ensureCapacity()用法及代碼示例
- Java Stack subList()用法及代碼示例
注:本文由純淨天空篩選整理自Code_r大神的英文原創作品 Stack remove(Object) method in Java with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。