- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to reverse the elements of an array using stack in java?
Stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example – a deck of cards or a pile of plates, etc.
A stack is first in first out, it has two main operations push and pop. Push inserts data in to it and pop retrieves data from it.
To reverse an array using stack initially push all elements in to the stack using the push() method then, retrieve them back using the pop() method into another array.
Example
import java.util.Arrays; import java.util.Stack; public class ReversinArrayUsingStack { public static void main(String args[]) { Stack<Integer> stack = new Stack<Integer>(); int[] myArray = {23, 93, 56, 92, 39}; int size = myArray.length; for(int i=0; i<size; i++) { stack.push(myArray[i]); } int[] reverseArray = new int[size]; for(int i=0; i<size; i++) { reverseArray[i] = stack.pop(); } System.out.println("Reversed array is ::"+Arrays.toString(reverseArray)); } }
Output
Reversed array is ::[39, 92, 56, 93, 23]
- Related Articles
- C program to reverse an array elements
- Python program to print the elements of an array in reverse order
- How to reverse an Array using STL in C++?
- How to create a stack in TypeScript using an array?
- C++ program to reverse an array elements (in place)
- Java program to reverse an array
- Golang Program to reverse the elements of the array using inbuilt function
- Reverse a Stack using C#
- Reverse a Stack using Queue
- How to print the elements in a reverse order from an array in C?
- Reverse a number using stack in C++
- Reverse an array using C#
- How do I reverse an int array in Java
- How to add elements to the midpoint of an array in Java?
- Python Program to Reverse a Stack using Recursion

Advertisements