Reversing An Array In Java: Everything You Need To Know About Reversing Arrays

Last updated on Jun 19,2023 1.5K Views

Reversing An Array In Java: Everything You Need To Know About Reversing Arrays

edureka.co

Reversing some data that lies in data structures, occasionally serves some meaningful purposes. We might find ourselves with the need to reverse an array in java, intermittently. There are various ways by which it can be done. In this article I would be discussing most relevant and noteworthy methods for reversing an array in Java.

We would learn three methods to achieve the above mentioned,

Let us get started with the first method,

Reversing An Array In Java

Method 1

/* Basic Java program that reverses an array*/
public class arrayReverse {   
/*function that reverses the array and stores it  
in another array*/
static void reverse(int a[], int n) 
{ 
int[] d = new int[n]; 
int j = n; 
for(int i = 0; i < n; i++) { 
d[j - 1] = a[i]; 
j = j - 1; 
}   
/*Printing the Reversed array*/
System.out.println("The Reversed array is: n"); 
for(int k = 0; k < n; k++) { 
System.out.println(d[k]); 
} 
} 
public static void main(String[] args) 
{ 
int [] array = {25, 28, 29, 18, 65}; 
reverse(array, array.length); 
} 
}

The program confers to the following steps:

All elements present in the array are placed in the new array in a reverse order. It must be noted that the new array is iterated from the last element.

Output:

Reversed array is: 

65

18

29

28

25

The method used is the most basic method for reversing an array and is widely used for its simplistic nature.

Method 2: Reversing An Array In Java

In the previous example, we had created a new array consisting of the reversed elements. This method, we will be reversing the original array by swapping the elements.

/*Java program that reverses an array using swaps*/
public class Main
{
public static void main(String[] args) {
int[] array = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
System.out.println("Array Before Reversing:");
/*function that reverses the array using swap*/ 
for(int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
} 
for (int i = 0; i < array.length / 2; i++) {
int temp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = temp;
} 
/*Printing the Reversed array*/
System.out.println("nArray After Reversing:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
} 
}
}

In the above example, the first element is swapped with the last element. Similarly, the second element is swapped with the penultimate element and so on.  For example, 1 is swapped with n, 2 is swapped with n-1 etc.

Output:

Array Before Reversing:

10 9 8 7 6 5 4 3 2 1

Array After Reversing:

1 2 3 4 5 6 7 8 9 10 

Let us move to the final bit of this article,

Method 3

This method reverses the array by converting the array to a List, after which it utilizes the Collections.reverse() method. The Collections.reverse() method obtains the list and reverses the elements. In the example given below, we create an ArrayList named array and add multiple elements to it. The Collections.reverse() method reverses the array in linear time.

import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList array = new ArrayList();
array.add("My");
array.add("Name");
array.add("Is");
array.add("Jeremy");
array.add("Hanson");
System.out.println("Before Reverse Order: " + array);
Collections.reverse(array);
System.out.println("After Reverse Order: " + array);
}
}

Output:

Before Reverse Order: [My, Name, Is, Jeremy, Hanson]

After Reverse Order: [Hanson, Jeremy, Is, Name, My]

These methods provide the most holistic approach for reversing an array in the programming language of Java.

Build real-world apps from scratch and showcase your skills with our hands-on Flutter APP Development Course.

Thus we have come to an end of this article on ‘Reversing An Array In Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

If you want to start a career in the Node JS Field then check out the Node JS Training Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of this blog  and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES