All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Reverse An Array In Java

Last Updated : Mar 11, 2024

How To Reverse An Array In Java

In this article we will show you the solution of how to reverse an array in java, we will explore two approaches to achieve this: using an auxiliary array and without using any extra space.

Reversing an array is a common programming task that involves rearranging the elements of an array in reverse order.

To reverse an array using an auxiliary array, we will create a new array of the same size as the original array.

We will iterate through the original array in reverse order and store each element in the new array. Finally, we will assign the reversed array back to the original array.

Step By Step Guide On How To Reverse An Array In Java :-

public class ArrayReversal {
    public static void reverseArray(int[] arr) {
        int[] reversedArray = new int[arr.length];
        int j = 0;
        for (int i = arr.length - 1; i >= 0; i--) {
            reversedArray[j] = arr[i];
            j++;
        }
        // Assign reversed array back to original array
        for (int i = 0; i < arr.length; i++) {
            arr[i] = reversedArray[i];
        }
    }
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        // Display original array
        System.out.println("Original Array: ");
        for (int element : array) {
            System.out.print(element + " ");
        }
        // Reverse the array
        reverseArray(array);
        // Display reversed array
        System.out.println("\nReversed Array: ");
        for (int element : array) {
            System.out.print(element + " ");
        }
    }
}
  1. We define a class called ArrayReversal and declare the reverseArray method that takes an integer array as a parameter.
  2. Within the reverseArray method, we create a new integer array called reversedArray of the same length as the original array.
  3. We initialize a variable j to 0, which will be used to track the index of the reversed array.
  4. Using a for loop, we iterate through the original array in reverse order starting from the last element.
  5. Inside the loop, we assign the current element of the original array to the corresponding index of the reversed array, incrementing j by 1 after each assignment.
  6. After the loop completes, the reversedArray will contain the reversed elements of the original array.
  7. To update the original array with the reversed elements, we use another for loop to assign each element of reversedArray back to the original array at the corresponding index.
  8. In the main method, we create an example array, array, and display its elements before the reversal.
  9. We then call the reverseArray method, passing the array as an argument to reverse its elements.
  10. Finally, we display the reversed array.

Conclusion :-

In this tutorial, we have learned how to reverse an array in Java. We explored the method of using an auxiliary array to store the reversed elements and assigned them back to the original array.

Reversing an array is an essential programming skill, and understanding this technique will be useful in various applications.

Remember to analyze the requirements of your program and choose the appropriate method accordingly.

I hope this article on how to reverse an array in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪