Java/J2EE and SOA (246 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (46 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (246 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

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

Published on Jul 08,2019 55 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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:

  • Input: The size and the elements of the array are taken as the input.

  • Reverse Function: The program makes use of the reverse function. The function takes the parameters: the array i.e. array and the size of the array i.e. n.

  • Methodology: In the function,  a new array, with the size of the first array, is initialized. The array array[] is iterated from the beginning.

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.

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.

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.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.