在Java中使用Example的Stack isEmpty()方法

在Java中使用Example的Stack isEmpty()方法

Java中的Java.util.Stack.isEmpty()

方法用于检查和验证Stack是否为空。如果Stack为空,则返回True,否则返回False。

语法:

Stack.isEmpty()

参数: 该方法不需要任何参数。

返回值: 该函数返回True,如果Stack为空,则返回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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程