Stack是后进先出 (LIFO) 数据结构。它支持两种基本操作,称为push 和pop。 push
操作在栈顶添加一个元素,pop
操作从栈顶移除一个元素。
Java 提供了一个 Stack 类,它对 Stack 数据结构进行建模。 Stack 类是 Java 集合框架的一部分。以下是 Java 中 Stack 的类层次结构 -
Stack 类扩展了 Vector
,它实现了 List
接口。 Vector 是一个可重新调整大小的集合。它会增大其大小以容纳新元素,并在删除元素时缩小其大小。
由于 Stack 类扩展了 Vector
,当添加或删除新元素时,它也会根据需要增加和缩小其大小。
import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
// Creating a Stack
Stack<String> stackOfCards = new Stack<>();
// Pushing new items to the Stack
stackOfCards.push("Jack");
stackOfCards.push("Queen");
stackOfCards.push("King");
stackOfCards.push("Ace");
System.out.println("Stack => " + stackOfCards);
System.out.println();
// Popping items from the Stack
String cardAtTop = stackOfCards.pop(); // Throws EmptyStackException if the stack is empty
System.out.println("Stack.pop() => " + cardAtTop);
System.out.println("Current Stack => " + stackOfCards);
System.out.println();
// Get the item at the top of the stack without removing it
cardAtTop = stackOfCards.peek();
System.out.println("Stack.peek() => " + cardAtTop);
System.out.println("Current Stack => " + stackOfCards);
}
}
# Output
Stack => [Jack, Queen, King, Ace]
Stack.pop() => Ace
Current Stack => [Jack, Queen, King]
Stack.peek() => King
Current Stack => [Jack, Queen, King]
import java.util.Stack;
public class StackSizeSearchExample {
public static void main(String[] args) {
Stack<String> stackOfCards = new Stack<>();
stackOfCards.push("Jack");
stackOfCards.push("Queen");
stackOfCards.push("King");
stackOfCards.push("Ace");
System.out.println("Stack : " + stackOfCards);
// Check if the Stack is empty
System.out.println("Is Stack empty? : " + stackOfCards.isEmpty());
// Find the size of Stack
System.out.println("Size of Stack : " + stackOfCards.size());
// Search for an element
// The search() method returns the 1-based position of the element from the top of the stack
// It returns -1 if the element was not found in the stack
int position = stackOfCards.search("Queen");
if(position != -1) {
System.out.println("Found the element \"Queen\" at position : " + position);
} else {
System.out.println("Element not found");
}
}
}
# Output
Stack : [Jack, Queen, King, Ace]
Is Stack empty? : false
Size of Stack : 4
Found the element "Queen" at position : 3
本节中的示例展示了迭代 Stack 的各种方法。
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Stack;
public class IterateOverStackExample {
public static void main(String[] args) {
Stack<String> stackOfPlates = new Stack<>();
stackOfPlates.add("Plate 1");
stackOfPlates.add("Plate 2");
stackOfPlates.add("Plate 3");
stackOfPlates.add("Plate 4");
System.out.println("=== Iterate over a Stack using Java 8 forEach() method ===");
stackOfPlates.forEach(plate -> {
System.out.println(plate);
});
System.out.println("\n=== Iterate over a Stack using iterator() ===");
Iterator<String> platesIterator = stackOfPlates.iterator();
while (platesIterator.hasNext()) {
String plate = platesIterator.next();
System.out.println(plate);
}
System.out.println("\n=== Iterate over a Stack using iterator() and Java 8 forEachRemaining() method ===");
platesIterator = stackOfPlates.iterator();
platesIterator.forEachRemaining(plate -> {
System.out.println(plate);
});
System.out.println("\n=== Iterate over a Stack from TOP to BOTTOM using listIterator() ===");
// ListIterator allows you to traverse in both forward and backward directions.
// We'll start from the top of the stack and traverse backwards.
ListIterator<String> platesListIterator = stackOfPlates.listIterator(stackOfPlates.size());
while (platesListIterator.hasPrevious()) {
String plate = platesListIterator.previous();
System.out.println(plate);
}
}
}
# Output
=== Iterate over a Stack using Java 8 forEach() method ===
Plate 1
Plate 2
Plate 3
Plate 4
=== Iterate over a Stack using iterator() ===
Plate 1
Plate 2
Plate 3
Plate 4
=== Iterate over a Stack using iterator() and Java 8 forEachRemaining() method ===
Plate 1
Plate 2
Plate 3
Plate 4
=== Iterate over a Stack from TOP to BOTTOM using listIterator() ===
Plate 4
Plate 3
Plate 2
Plate 1
在本文中,您了解了什么是 Stack,如何在 Java 中创建 Stack,如何在 Stack 中执行 push 和 pop 操作,如何检查 Stack 是否为空,如何找到 Stack 的大小以及如何搜索Stack中的元素。