5 Best Ways to Reverse an Array in Java

0
20

In this tutorial, We will learn how to reverse an Array in Java by using a simple for loop, by using ArrayList, by using StringBuilder.append () and by using ArrayUtils.reverse ().

What is an Array in Java?

An Array is a collection object which contains multiple elements of the same type. It provides contiguous memory allocation and fixed size of elements. It is also based on indexing which starts from “0”.

Let’s see the below example of an Array with a total of 8 elements where the first index starts from “0” and the last index is “7”. We have differentiated indexes as indices and values ​​as elements. The length of the below Array is 8.

You can also find any particular elements of an Array in Java by using a simple if condition or for loop.

Example of String Array

String[] str = "Let's", "learn", "Array", "in", "Java";

Code language: JavaScript (javascript)

Example of Integer Array

int[] num = 45, 25, 65, 8, 47, 5;

Why do we need to reverse an Array in Java?

In the real-time project, we need to access the elements of an Array from the last index to the first index. To retrieve elements of the Array from the last index to the first index, it’s recommended to reverse an Array and then use the elements of that Array.

In most of the MNC’s interviews or coding round, Companies also ask the same type of questions like how to reverse an Array in java without using the predefined methods. So it’s very important to know these concepts before attending the interviews.

Let’s understand how we can reverse an Array in Java.

How to reverse an Array in Java?

We can reverse an Array in Java by using the following methods.

  1. Reverse an Array by using for loop.
  2. Reverse an Array by using in Place method (Swapping Method)
  3. Reverse an Array by using ArrayList and Collections.reverse () method
  4. Reverse an Array by using StringBuilder.append () method
  5. Reverse an Array by using ArrayUtils.reverse () method
Example of Reverse an Array in Java
Example of Reverse an Array in Java

Reverse an Array by using for loop

We can reverse an Array in Java by using a simple for loop. We will iterate through the given Array from the last index to the first index and will store the elements in the new Array one by one.

Once the iteration will be completed, the elements of the Array are also reversed and finally, we can display the reversed Array to the user.

Let’s see the complete program here.

package programs; import java.util.Arrays; public class ReverseAnArrayUsingForLoop public static void revArray(String givenArray[]) int strLen = givenArray.length; String revArray[] = new String[strLen]; int temp = strLen; for (int i = 0; i < strLen; i++) revArray[i] = givenArray[temp - 1]; temp--; System.out.println("Reversed Array is " + Arrays.deepToString(revArray)); public static void main(String[] args) String strArray[] = "Codingface", "is", "a", "Programming", "Tutorial", "Portal" ; revArray(strArray);

Code language: JavaScript (javascript)

Output

Reversed Array is [Portal, Tutorial, Programming, a, is, Codingface]

Code language: JavaScript (javascript)

2. Reverse an Array by using in Place method (Swapping Method)

We can reverse an Array in Java by using the in-place method. In this method, we can reverse the Array just by swapping the elements with each other and without using any other predefined methods or another Array. Like, we will swap the last element with the first element within the same Array and 2nd last element with 2nd element, and so on. Finally, the whole Array will be reversed one by one.

Let’s see the complete program here.

package programs; import java.util.Arrays; public class ReverseAnArrayUsingInPlaceMethod public static void revArray(String givenArray[]) String temp; for (int i = 0; i < givenArray.length / 2; i++) temp = givenArray[i]; givenArray[i] = givenArray[givenArray.length - i - 1]; givenArray[givenArray.length - i - 1] = temp; System.out.println("Reversed Array is " + Arrays.toString(givenArray)); public static void main(String[] args) String strArray[] = "Codingface", "is", "a", "Programming", "Tutorial", "Portal" ; revArray(strArray);

Code language: JavaScript (javascript)

Output

Reversed Array is [Portal, Tutorial, Programming, a, is, Codingface]

Code language: JavaScript (javascript)

Reverse an Array by using ArrayList and Collections.reverse () method

We can reverse an Array in Java by using the Collections.reverse () method.

As we can not directly reverse an Array by using the Collections.reverse () method, So first we will convert the given Array to a List, and then we will reverse the given Array by using the Collections.reverse () method.

Let’s see the complete program to understand the above logic.

package programs; import java.util.Arrays; import java.util.Collections; public class ReverseAnArrayUsingCollectionsReverseFunction public static void revArray(String givenArray[]) Collections.reverse(Arrays.asList(givenArray)); System.out.println("Reversed Array is " + Arrays.asList(givenArray)); public static void main(String[] args) String strArray[] = "Codingface", "is", "a", "Programming", "Tutorial", "Portal" ; revArray(strArray);

Code language: JavaScript (javascript)

Output

Reversed Array is [Portal, Tutorial, Programming, a, is, Codingface]

Code language: JavaScript (javascript)

4. Reverse an Array by using StringBuilder.append () method

We can reverse an Array in Java by using the StringBuilder.append () method.

First, we will convert the given Array to a StringBuilder by using the append () method. Once it will be converted to StringBuilder, we will convert it to an Array by using the String split () method.

Let’s see the complete program to understand the above logic.

package programs; import java.util.Arrays; import java.util.Collections; public class ReverseAnArrayUsingCollectionsReverseFunction public static void revArray(String givenArr[]) StringBuilder sb = new StringBuilder(); for (int i = givenArr.length; i > 0; i--) sb.append(givenArr[i - 1]).append(" "); String[] revArray = givenArr.toString().split(" "); System.out.println("Reversed Array is " + Arrays.toString(revArray)); public static void main(String[] args) String strArray[] = "Codingface", "is", "a", "Programming", "Tutorial", "Portal" ; revArray(strArray);

Code language: JavaScript (javascript)

Output

Reversed Array is [Portal, Tutorial, Programming, a, is, Codingface]

Code language: JavaScript (javascript)

5. Reverse an Array by using ArrayUtils.reverse () method

We can reverse an Array in Java by using the ArrayUtils.reverse () method.

We can directly reverse the given string by using the ArrayUtils.reverse () method. You can just pass the given Array as an argument for the reverse () method.

Let’s see the complete program to understand the above logic.

package programs; import java.util.Arrays; import java.util.Collections; import org.apache.commons.lang3.ArrayUtils; public class ReverseAnArrayUsingArrayUtilsReverse public static void revArray(String givenArray[]) ArrayUtils.reverse(givenArray); System.out.println("Reversed Array is " + Arrays.toString(givenArray)); public static void main(String[] args) String strArray[] = "Codingface", "is", "a", "Programming", "Tutorial", "Portal" ; revArray(strArray);

Code language: JavaScript (javascript)

Output

Reversed Array is [Portal, Tutorial, Programming, a, is, Codingface]

Code language: JavaScript (javascript)

Conclusion

In this tutorial we have learned how to reverse an Array in Java by using different methods like using for loop, in-place method, ArrayList, StringBuilder.append () method and ArrayUtils.reverse () method.

You can use any of the above approach to reverse your Array. We have taken the examples of only String Arrays where as you can also try for the int Arrays. You just need to replace the String Arrays with int Array without any other changes.

Hope, you can be able to achieve your requirement and liked our tutorial. If our tutorial helped you or if you liked our tutorial, then please share with others.

Thanks & Happy Learning!

Recommended Articles

  1. 4 Best Ways To Print Array Without Brackets In Java.
  2. Best 2 Ways To Find The Nth Element In Array Java.
  3. How To Convert List To Byte Array In Java? – Easy Way
  4. How To Convert Array To XML In Java – 2 Easy Way

FAQs

How to reverse the order of an array in java?

We can reverse the order of an Array in Java by following methods:
Reverse an Array by using a simple for loop through iteration.
2. Reverse an Array by using the in-place method.
Reverse an Array by using ArrayList and Collections.reverse () method
4. Reverse an Array by using StringBuilder.append () method
5. Reverse an Array by using ArrayUtils.reverse () method

Is there any predefined methods that we can use to reverse an Array?

Yes, we can use predefined methods like Collections.reverse () and ArrayUtils.reverse () methods to reverse an Array in Java.

Can we reverse both Integer and String Array?

Yes, We can reverse both Integer and String Arrays in Java. The logic will be the same for both of them but we just need to define both Arrays in different programs.

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here