Java中的Java.util.Stack.isEmpty()方法用於檢查和驗證Stack是否為空。如果堆棧為空,則返回True,否則返回False。
用法:
Stack.isEmpty()
參數:此方法不帶任何參數。
返回值:如果Stackis為空,則此函數返回True,否則返回False。
以下示例程序旨在說明Java.util.Stack.isEmpty()方法:
示例1:
// Java code to illustrate isEmpty()
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 into the Stack
stack.add("Welcome");
stack.add("To");
stack.add("Geeks");
stack.add("4");
stack.add("Geeks");
// Displaying the Stack
System.out.println("Stack: " + stack);
// Verifying if the Stack is empty or not
System.out.println("Is the Stack empty? "
+ stack.isEmpty());
// Clearing the Stack
stack.clear();
// Displaying the Stack
System.out.println("Stack after clear(): "
+ stack);
// Verifying if the Stack is empty or not
System.out.println("Is the Stack empty? "
+ stack.isEmpty());
}
}
輸出:
Stack: [Welcome, To, Geeks, 4, Geeks] Is the Stack empty? false Stack after clear(): [] Is the Stack empty? true
示例2:
// Java code to illustrate isEmpty()
import java.util.*;
public class StackDemo {
public static void main(String args[])
{
// Creating an empty Stack
Stack<Integer> stack = new Stack<Integer>();
// Displaying the Stack
System.out.println("Stack: " + stack);
// Verifying if the Stack is empty or not
System.out.println("Is the Stack empty? "
+ stack.isEmpty());
}
}
輸出:
Stack: [] Is the Stack empty? true
相關用法
- Java CopyOnWriteArraySet isEmpty()用法及代碼示例
- Java ArrayDeque isEmpty()用法及代碼示例
- Java Dictionary isEmpty()用法及代碼示例
- Java BitSet isEmpty()用法及代碼示例
- Java Map isEmpty()用法及代碼示例
- Java HashSet isEmpty()用法及代碼示例
- Java Vector isEmpty()用法及代碼示例
- Java Set isEmpty()用法及代碼示例
- Java ConcurrentLinkedQueue isEmpty()用法及代碼示例
- Java String isEmpty()用法及代碼示例
- Java HashMap isEmpty()用法及代碼示例
- Java Hashtable isEmpty()用法及代碼示例
- Java ConcurrentHashMap isEmpty()用法及代碼示例
- Java NavigableSet isEmpty()用法及代碼示例
- Java LinkedHashSet isEmpty()用法及代碼示例
注:本文由純淨天空篩選整理自Code_r大神的英文原創作品 Stack isEmpty() method in Java with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。