W tym samouczku dowiemy się o klasie Java Stack i jej metodach na przykładach.
Struktura kolekcji Java ma klasę o nazwie, Stack
która zapewnia funkcjonalność struktury danych stosu.
Stack
Klasa rozszerza Vector
klasę.
Implementacja stosu
W stosie elementy są przechowywane i dostępne w trybie Last In First Out . Oznacza to, że elementy są dodawane na wierzchu stosu i usuwane z góry stosu.
Tworzenie stosu
Aby utworzyć stos, musimy najpierw zaimportować java.util.Stack
pakiet. Po zaimportowaniu pakietu, oto jak możemy utworzyć stos w Javie.
Stack stacks = new Stack();
Tutaj Type
wskazuje typ stosu. Na przykład,
// Create Integer type stack Stack stacks = new Stack(); // Create String type stack Stack stacks = new Stack();
Metody stosu
Ponieważ Stack
rozszerza Vector
klasę, dziedziczy wszystkie metody Vector
. Aby dowiedzieć się więcej o różnych Vector
metodach, odwiedź stronę Java Vector Class.
Oprócz tych metod Stack
klasa zawiera 5 innych metod, które ją odróżniają Vector
.
push () Metoda
Aby dodać element na szczyt stosu, używamy push()
metody. Na przykład,
import java.util.Stack; class Main ( public static void main(String() args) ( Stack animals= new Stack(); // Add elements to Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Stack: " + animals); ) )
Wynik
Stos: (Pies, Koń, Kot)
pop () Metoda
Aby usunąć element ze szczytu stosu, używamy pop()
metody. Na przykład,
import java.util.Stack; class Main ( public static void main(String() args) ( Stack animals= new Stack(); // Add elements to Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Initial Stack: " + animals); // Remove element stacks String element = animals.pop(); System.out.println("Removed Element: " + element); ) )
Wynik
Stos początkowy: (pies, koń, kot) Usunięty element: Cat
peek () Metoda
peek()
Sposób powraca obiektu z górnej części stosu. Na przykład,
import java.util.Stack; class Main ( public static void main(String() args) ( Stack animals= new Stack(); // Add elements to Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Stack: " + animals); // Access element from the top String element = animals.peek(); System.out.println("Element at top: " + element); ) )
Wynik
Stos: (Pies, Koń, Kot) Element u góry: Cat
search () Metoda
Aby wyszukać element w stosie, używamy search()
metody. Zwraca pozycję elementu z góry stosu. Na przykład,
import java.util.Stack; class Main ( public static void main(String() args) ( Stack animals= new Stack(); // Add elements to Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Stack: " + animals); // Search an element int position = animals.search("Horse"); System.out.println("Position of Horse: " + position); ) )
Wynik
Stos: (pies, koń, kot) Pozycja konia: 2
empty () Metoda
Aby sprawdzić, czy stos jest pusty, używamy empty()
metody. Na przykład,
import java.util.Stack; class Main ( public static void main(String() args) ( Stack animals= new Stack(); // Add elements to Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Stack: " + animals); // Check if stack is empty boolean result = animals.empty(); System.out.println("Is the stack empty? " + result); ) )
Wynik
Stos: (Pies, Koń, Kot) Czy stos jest pusty? fałszywy
Użyj ArrayDeque zamiast Stack
Stack
Klasa zapewnia bezpośrednią realizację struktury danych stos. Jednak zaleca się, aby go nie używać. Zamiast tego użyj ArrayDeque
klasy (implementuje Deque
interfejs), aby zaimplementować strukturę danych stosu w Javie.
Aby dowiedzieć się więcej, odwiedź:
- Java ArrayDeque
- Dlaczego warto korzystać z Deque over Stack?