From 56588a0cd449a0bc84a0472b6dadb62b6c4ed3a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Stevanovi=C4=87?= Date: Tue, 2 Jul 2019 03:51:19 +0200 Subject: [PATCH] Added Stack class (#33956) Added Stack class, what's about and methods being used inside of it. --- guide/english/java/collections/index.md | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/guide/english/java/collections/index.md b/guide/english/java/collections/index.md index 389b629a1b..d624eb3d94 100644 --- a/guide/english/java/collections/index.md +++ b/guide/english/java/collections/index.md @@ -123,6 +123,53 @@ intList.removeFirst(); // Removes first element of the list intList.removeLast(); // Removes last element of the list ``` +## The Stack Class + +Stack class, as previously mentioned classes, implements Collection interface and extends Vector class and contains five methods that treat Vector as Stack. Stack is data structure which applies principle Last In First Out (LIFO) and contains following methods: + +```java + +T void pop(); // 'T' represents type which is being returned as the last elemented is removed from this stack + +T void peek(); // 'T' represents type which is being returned and last elemented is not being removed from this stack + +T push(T element); // 'T' represents type which is being returned and last elemented is being added on this stack + +boolean empty(); // returns true (if this stack is empty) of false (if this stack isn't empty) + +int search(Object o) // returns position of element located on this stack +``` + +**Create a Stack** + +Stack class is being created using following snippet of code: + +```java +Stack stackObject = new Stack<>(); // creates stack which recieves elements of Integer/int type +``` + +**Add elements on Stack** + +```java + +stackObject.push(5); +stackObject.push(2); +stackObject.push(36); +stackObject.push(4); + +``` + +**Retrieve element from Stack** + +```java + +int lastElemement = stackObject.pop(); // retrieves last element pushed on top of 'stackObject' stack and REMOVES it +System.out.println(lastElemement); // prints retrieved element which is 4 + +lastElements = stackObject.peek(); // retrieves last element pushed on top of 'stackObject' stack and DOES NOT REMOVE it +System.out.println(lastElemement); // prints retrieved element which is 36 +``` + Note: All the above mentioned methods for removing and fetching an element return `NoSuchElementException` on an empty list. #### More Information