Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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,
/* 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.
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,
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.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 1st March,2025 1st March SAT&SUN (Weekend Batch) | View Details |
edureka.co