Java 中的 Stack empty() 方法
Java 中的 java.util.Stack.empty() 方法用于检查堆栈是否为空。该方法的返回值类型是布尔类型,并在堆栈为空时返回 true,否则返回 false。
语法:
STACK.empty()
参数: 该方法不需要任何参数。
返回值: 如果堆栈为空,方法返回布尔值 true,否则返回 false。
下面的程序演示了 java.util.Stack.empty() 方法的工作过程:
程序 1:
// Java code to demonstrate empty() method
import java.util.*;
public class Stack_Demo {
public static void main(String[] args)
{
// Creating an empty Stack
Stack<String> STACK = new Stack<String>();
// Stacking strings
STACK.push("Geeks");
STACK.push("4");
STACK.push("Geeks");
STACK.push("Welcomes");
STACK.push("You");
// Displaying the Stack
System.out.println("The stack is: " + STACK);
// Checking for the emptiness of stack
System.out.println("Is the stack empty? " +
STACK.empty());
// Popping out all the elements
STACK.pop();
STACK.pop();
STACK.pop();
STACK.pop();
STACK.pop();
// Checking for the emptiness of stack
System.out.println("Is the stack empty? " +
STACK.empty());
}
}
输出
The stack is: [Geeks, 4, Geeks, Welcomes, You]
Is the stack empty? false
Is the stack empty? true
程序 2:
// Java code to demonstrate empty() method
import java.util.*;
public class Stack_Demo {
public static void main(String[] args)
{
// Creating an empty Stack
Stack<Integer> STACK = new Stack<Integer>();
// Stacking int values
STACK.push(8);
STACK.push(5);
STACK.push(9);
STACK.push(2);
STACK.push(4);
// Displaying the Stack
System.out.println("The stack is: " + STACK);
// Checking for the emptiness of stack
System.out.println("Is the stack empty? " +
STACK.empty());
}
}
输出
The stack is: [8, 5, 9, 2, 4]
Is the stack empty? false
程序 3: 如何使用 java.util.Stack.empty() 方法遍历堆栈以获取所有元素的总和
// 演示使用empty()方法遍历栈的Java代码
import java.util.*;
public class Stack_Demo {
public static void main(String[] args)
{
// 创建一个空栈
Stack<Integer> STACK = new Stack<>();
// 也可以像下面这样初始化栈
// 两种方式都是一样的
// Stack<Integer> STACK = new Stack<Integer>();
// 将元素压入栈
STACK.push(23);
STACK.push(3);
STACK.push(-30);
STACK.push(13);
STACK.push(45);
// 显示栈
System.out.println("栈是:" + STACK);
// 初始化一个变量,用于存储所有元素的总和
int sum = 0;
// 不断弹出栈顶元素直到栈为空
while (!STACK.empty()) {
/* 如果栈中有元素,则Stack.empty()为false,
!Stack.empty()将为true,因此while循环将一直进行,
直到!STACK.empty()为false,也就是栈为空然后退出循环*/
sum += STACK.pop();
}
// 初始化
System.out.println("所有元素的总和是 " + sum);
// 检查栈是否为空
System.out.println("栈是否为空? "
+ STACK.empty());
}
// 代码由Vikas Bishnoi提供
}
输出
栈是:[23, 3, -30, 13, 45]
所有元素的总和是 54
栈是否为空? true
程序4: 使用java.util.Stack.empty()方法实现 错误处理 。
例如,如果从文件中读取数据并将其存储到栈中,则可以在读取数据后检查栈是否为空,以确保文件不是空文件。
// 演示使用empty()方法进行错误处理的Java代码
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;
public class FileReadExample {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
// 从文件中读取数据
try {
File file = new File("data.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
stack.push(line);
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("找不到文件。");
}
// 检查栈是否为空
if (stack.empty()) {
System.out.println("文件为空。");
} else {
System.out.println("文件不为空。栈内容:");
while (!stack.empty()) {
System.out.println(stack.pop());
}
}
}
}
// 代码由vishalkumarsahu04提供```
时间复杂度: O(n),其中n是文件中的行数。
空间复杂度: O(n),其中n是文件中的行数。