
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Reverse an Array
In this article, we will learn to reverse an array in Java. Reversing an array is a classic problem that helps understand essential concepts like data structures and in-place manipulation. This operation is commonly required in programming tasks, such as data transformations, algorithm implementations, and solving logical problems.
Problem Statement
Reversing an array involves rearranging its elements so that the first element becomes the last, the second becomes the second-to-last, and so on.
Input
int[] myArray = {23, 93, 56, 92, 39};
Output
{39, 92, 56, 93, 23}
Different Approaches
Following are the two different approaches to reverse an array in Java ?
Using a Stack
A stack is first in first out, it has two main operations push and pop. Push inserts data into it and pop retrieves data from it. To reverse an array using stack initially push all elements into the stack using the push() method then, retrieve them back using the pop() method into another array.
Following are the steps to reverse an array using the stack ?
-
Stack Initialization: A stack is used to store the elements of the input array temporarily.
-
Push Operation: All elements of the array are pushed onto the stack one by one.
-
Pop Operation: Elements are popped from the stack and stored in a new array. This reverses the order since the stack operates on LIFO.
- Output: The reversed array is printed using Arrays.toString().
for(int i=0; i<size; i++) { reverseArray[i] = stack.pop(); }
Example
Below is an example of reversing an array using the stack ?
import java.util.Arrays; import java.util.Stack; public class ab38_ReverseOfArray { 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]
Using Two-Pointer Technique
This is a more efficient approach as it avoids using extra space for a stack. The two-pointer technique swaps elements from the beginning and the end of the array until the pointers meet in the middle.
Following are the steps to reverse an array using the pointer technique ?
- Two-Pointer Setup: Two pointers, start and end, are initialized to the first and last indices of the array.
- Swap Operation: Elements at the positions of start and end are swapped.
- Pointer Movement: The start pointer is incremented, and the end pointer is decremented, moving toward the center.
- Output: The array is reversed in place without using additional memory for storage.
using a while loop to swap elements at the start and end ?
while (start < end) { int temp = myArray[start]; myArray[start] = myArray[end]; myArray[end] = temp; }
Example
Below is an example of reversing an array using the pointer technique ?
import java.util.Arrays; public class ReverseArray { public static void main(String[] args) { int[] myArray = {23, 93, 56, 92, 39}; // Reverse the array in-place using two pointers int start = 0; int end = myArray.length - 1; while (start < end) { // Swap elements at start and end int temp = myArray[start]; myArray[start] = myArray[end]; myArray[end] = temp; // Move the pointers start++; end--; } // Display the reversed array System.out.println("Reversed array is :: " + Arrays.toString(myArray)); } }
Output
Reversed array is ::[39, 92, 56, 93, 23]
Comparison Table
Aspect | Using Stack | Two-Pointer Technique |
Space Complexity | O(n) (extra space for the stack) | O(1) (in-place reversal) |
Time Complexity | O(n) | O(n) |
Ease of Implementation | Simple, especially with stack usage | Direct and efficient |
Memory Usage | Requires additional memory for the stack | No additional memory required |
Performance for Large Arrays | May slow down due to stack memory overhead | Performs better due to low memory usage |