class Stack<E> extends
class Vector<E> with the algorithm "last in - first out":
Each new element is placed at the very top.
The first element is the one at the very top.
Stack is fully synchronized (thread-safe).
Class
Stack extends class
Vector with five functions that allow a vector to be treated as a stack:
Java
push(E item) // Add a new element and the element is placed at the very top
E pop() // Pull out the element, take the element that is at the very top and delete it
E peek() // Look at the element that is at the very top, the element is not deleted
boolean empty() // checks if this stack is empty
int search(Object o) // Look for the item, the position is returned starting with 1
Java
Creating a new Java console application ... and write the code
package org.example;
import java.util.Stack;
public class Program {
public static void main(String[] args)
{
Stack<String> myList1 = new Stack<String>();
// Add the element to the top
myList1.push("Car");
// myList1 = ["Car"]
// Add the element to the top
myList1.push("House");
// myList1 = ["Car", "House"]
// Add the element to the top
myList1.push("Book");
// myList1 = ["Car", "House", "Book"]
// View item at the top
String elem = myList1.peek();
// elem = "Book"
// myList1 = ["Car", "House", "Book"]
// Take the element from the top, the element is deleted
elem = myList1.pop();
// elem = "Book"
// myList1 = ["Car", "House"]
/////// can call Vector functions /////////
// Add an element to the end
myList1.add("Notebook");
// myList1 = ["Car", "House", "Notebook"]
// View item by index
elem = myList1.get(1);
// elem = "House"
// myList1 = ["Car", "House", "Notebook"]
// Insert element by index 1
myList1.add(1, "Airplane");
// myList1 = ["Car", "Airplane", "House", "Notebook"]
// Delete item by index
myList1.remove(1);
// myList1 = ["Car", "House", "Notebook"]
}
}
Kotlin
Creating a new Kotlin console application ... and write the code
import java.util.Stack
fun main() {
val myList1 = Stack<String>()
// Add the element to the top
myList1.push("Car")
// myList1 = ["Car"]
// Add the element to the top
myList1.push("House")
// myList1 = ["Car", "House"]
// Add the element to the top
myList1.push("Book")
// myList1 = ["Car", "House", "Book"]
// View item at the top
var elem = myList1.peek()
// elem = "Book"
// myList1 = ["Car", "House", "Book"]
// Take the element from the top, the element is deleted
elem = myList1.pop()
// elem = "Book"
// myList1 = ["Car", "House"]
/////// can call Vector functions /////////
// Add an element to the end
myList1.add("Notebook")
// myList1 = ["Car", "House", "Notebook"]
// View item by index
elem = myList1[1]
// elem = "House"
// myList1 = ["Car", "House", "Notebook"]
// Insert element by index 1
myList1.add(1, "Airplane")
// myList1 = ["Car", "Airplane", "House", "Notebook"]
// Delete item by index
myList1.removeAt(1)
// myList1 = ["Car", "House", "Notebook"]
}
Java
class Stack<E> extends Vector<E>
{
public Stack() {
}
public E push(E item) {
addElement(item);
return item;
}
public synchronized E pop() {
E obj;
int len = size();
obj = peek();
removeElementAt(len - 1);
return obj;
}
public synchronized E peek() {
int len = size();
if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
}
public boolean empty() {
return size() == 0;
}
public synchronized int search(Object o) {
int i = lastIndexOf(o);
if (i >= 0) {
return size() - i;
}
return -1;
}
}
Learn more about methods in the Java documentation: