This write-up will provide a complete understanding of how to reverse an array in java, and in this regard, it will cover the following ways of reversing an array:
- How to Print an Array in Reverse Order
- How to Reverse an Array Using Collections.Reverse() Method
- How to Reverse an Array Using Swapping
Let’s get started!
How to Print an Array in Reverse Order
To print a reverse array, we can use the simple for loop and traverse it in reverse order (i.e., start it from the last index). It wouldn’t reverse the array’s order; instead, it will only print/display the array in reverse order.
Example
In the below snippet, we will create an integer type array of ten elements, and we will print it in reverse order:
In the above snippet, we initialize the loop with “arry.length – 1”, which represents that loop will start from the last index of the array. The condition “i >= 0” represents that the loop will terminate when the value of “i” becomes less than zero while “i–” means in each iteration the value of “i” will decrement by 1:
The above snippet shows that the array is successfully printed into reverse order.
How to Reverse an Array Using Collections.reverse() Method
In Java, Collections class provides a rverse() method which can be used to reverse the java arrays.
Example
In this example, we will create a string type array of five elements, and we will print it in reverse order using Collections.reverse() method:
static void reverseArray(String ary[]) {
Collections.reverse(Arrays.asList(ary));
System.out.println("Reverse Array: ");
System.out.println(Arrays.asList(ary));
}
public static void main(String[] args) {
String[] arry = {"Java", "PHP", "C#", "C++", "JavaScript"};
reverseArray(arry);
}
}
We created a reverseArray function to reverse the original array. Within the reverseArray function, we utilized the asList() method of the Arrays class to convert the array into a list. Then we passed it to the Collections.reverse() method, which will reverse the order of the original array:
The output authenticates the working of the Collections.reverse() method as it succeeded to reverse the original array.
How to Reverse an Array Using Swapping
In java, another frequently used way to reverse the array is by swapping its elements.
Example
The below snippet will let you understand how to reverse an array by swapping its elements in Java:
static void reverseArray(int ary[]) {
int len = ary.length;
int temp;
for (inti = 0; i<len / 2; i++) {
temp = ary[i];
ary[i] = ary[len - i - 1];
ary[len - i - 1] = temp;
}
System.out.println("Reversed Array: ");
for (int j = 0; j <len; j++) {
System.out.println(ary[j]);
}
}
public static void main(String[] args) {
int[] arry = {40, 34, 63, 14, 5};
reverseArray(arry);
}
}
In this example, we swapped the array’s first element with the last element; array’s second element with the second last element, and so on:
The output verified that the swapping process successfully reversed the array elements.
Conclusion
In Java, Collections.reverse() method, StringBuilder.append() method, swapping approach, etc. are used to print an array in reverse order. Moreover, to print an array in reverse order, we can use the traditional for loop and traverse it in reverse order. However, it wouldn’t reverse the array order; instead, it will only print the array in reverse order. In this write-up we discussed three different techniques to reverse an array in Java. For profound understanding, we utilized different data types such as String, integer etc, and presented the descriptive screenshots of the code snippets.