堆栈是一种后进先出 (LIFO) 数据结构。它支持两种基本操作,称为 push 和 pop。 push
操作在栈顶添加一个元素,而 pop
操作从栈顶移除一个元素。
Java 提供了一个 Stack 类,它对 Stack 数据结构进行建模。 Stack 类是 Java 集合框架的一部分。以下是Java中Stack的类层次结构
Stack 类扩展了实现 List
接口的 Vector
。 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
本节中的示例显示了迭代堆栈的各种方法。
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();
while (platesIterator.hasNext()) {
String plate = platesIterator.next();
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 中搜索元素。
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.dailycodebuffer.com/java-stack-class-tutorial/
内容来源于网络,如有侵权,请联系作者删除!