Java Stack Class
Java Tutorial
- Java Tutorial
- Java Introduction
- Java Development Environment Setup
- Java Basic Syntax
- Java Comments
- Java Objects and Classes
- Java Basic Data Types
- Java Variable Types
- Java Variable Naming Rules
- Java Modifiers
- Java Operators
- Java Loop Structures
- Java Conditional Statements
- Java switch case
- Java Number & Math Classes
- Java Character Class
- Java String Class
- Java StringBuffer
- Java Arrays
- Java Date Time
- Java Regular Expressions
- Java Methods
- Java Constructors
- Java Stream, File, IO
- Java Scanner Class
- Java Exception Handling
Java Object-Oriented
- Java Inheritance
- Java Override/Overload
- Java Polymorphism
- Java Abstract Classes
- Java Encapsulation
- Java Interfaces
- Java Enums
- Java Packages
- Java Reflection
Java Advanced Tutorial
- Java Data Structures
- Java Collections Framework
- Java ArrayList
- Java LinkedList
- Java HashSet
- Java HashMap
- Java Iterator
- Java Object
- Java NIO Files
- Java Generics
- Java Serialization
- Java Network Programming
- Java Sending Email
- Java Multithreading Programming
- Java Applet Basics
- Java Documentation Comments
- Java Examples
- Java 8 New Features
- Java MySQL Connection
- Java 9 New Features
- Java Quiz
- Java Common Libraries
Java Stack Class
Stack is a subclass of Vector that implements a standard last-in-first-out (LIFO) stack.
Stack only defines a default constructor, which creates an empty stack. In addition to all the methods defined by Vector, Stack also defines some of its own methods.
Stack()
In addition to all the methods defined by Vector, it also defines some methods:
| No. | Method Description |
|---|---|
| 1 | boolean empty() Tests if this stack is empty. |
| 2 | Object peek() Looks at the object at the top of this stack without removing it from the stack. |
| 3 | Object pop() Removes the object at the top of this stack and returns that object as the value of this function. |
| 4 | Object push(Object element) Pushes an item onto the top of this stack. |
| 5 | int search(Object element) Returns the 1-based position of an object on this stack. |
Example
The following program demonstrates several methods supported by this collection.
Example
import java.util.*;
public class StackDemo{
static void showpush(Stack<Integer> st, int a){
st.push(new Integer(a));
System.out.println("push(" + a + ")");
System.out.println("stack: " + st);
}
static void showpop(Stack<Integer> st){
System.out.print("pop -> ");
Integer a = (Integer)st.pop();
System.out.println(a);
System.out.println("stack: " + st);
}
public static void main(String args[]){
Stack<Integer> st = new Stack<Integer>();
System.out.println("stack: " + st);
showpush(st, 42);
showpush(st, 66);
showpush(st, 99);
showpop(st);
showpop(st);
showpop(st);
try {
showpop(st);
} catch (EmptyStackException e) {
System.out.println("empty stack");
}
}
}
The above example compiles and runs resulting in:
stack:
push(42)
stack:
push(66)
stack: [42, 66]
push(99)
stack: [42, 66, 99]
pop -> 99
stack: [42, 66]
pop -> 66
stack:
pop -> 42
stack:
pop -> empty stack
YouTip