0% found this document useful (0 votes)
0 views3 pages

Java Stack Class

The Java Stack class is a subclass of Vector that implements a last-in, first-out (LIFO) stack of objects. It includes methods for stack operations such as empty(), peek(), pop(), push(), and search(), along with a default constructor to create an empty stack. An example program demonstrates the use of these methods to manipulate stack elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views3 pages

Java Stack Class

The Java Stack class is a subclass of Vector that implements a last-in, first-out (LIFO) stack of objects. It includes methods for stack operations such as empty(), peek(), pop(), push(), and search(), along with a default constructor to create an empty stack. An example program demonstrates the use of these methods to manipulate stack elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Page 1 of 6

Home Whiteboard AI Assistant Online Compilers Jobs Tools Art

SQL HTML CSS Javascript Python Java C C++ PHP Scala C#

Java Stack Class

Introduction
Stack is a subclass of Vector that implements a standard last-in, first-out stack.

Stack only defines the default constructor, which creates an empty stack. Stack includes
all the methods defined by Vector, and adds several of its own.

The Java Stack class represents a last-in-first-out (LIFO) stack of objects.

When a stack is first created, it contains no items.

In this class, the last element inserted is accessed first.

Class declaration
Following is the declaration for java.util.Stack class −

public class Stack<E>


extends Vector<E>

Class constructors

Sr.No. Constructor & Description

Stack()
1
This constructor creates an empty stack.

Class methods
Page 2 of 6

Sr.No. Method & Description

boolean empty()
1
This method tests if this stack is empty.

E peek()
2 This method looks at the object at the top of this stack without removing it
from the stack.

E pop()
3 This method removes the object at the top of this stack and returns that
object as the value of this function.

E push(E item)
4
This method pushes an item onto the top of this stack.

int search(Object o)
5
This method returns the 1-based position where an object is on this stack.

Methods inherited
This class inherits methods from the following classes −

java.util.Vector

java.util.Collection
java.util.Object

java.util.List

Example
The following program illustrates several of the methods supported by Stack collection −

Open Compiler

import java.util.*;
public class StackDemo {

static void showpush(Stack st, int a) {


st.push(new Integer(a));
System.out.println("push(" + a + ")");
System.out.println("stack: " + st);
}
Page 3 of 6

static void showpop(Stack 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 st = new Stack();
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");
}
}
}

This will produce the following result −

Output

stack: [ ]
push(42)
stack: [42]
push(66)
stack: [42, 66]
push(99)
stack: [42, 66, 99]
pop -> 99
stack: [42, 66]
pop -> 66
stack: [42]
pop -> 42

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy