How to Reverse an Array in Java? Integer and String Array Example Tutorial (original) (raw)
This Java tip is about, how to reverse an array in Java, mostly primitive types e.g. int, long, double and String arrays. Despite of Java’s rich Collection API, use of array is quite common, but standard JDK doesn’t have great utility classes for Java. It’s difficult to convert between Java Collection e.g. List,Setto primitive arrays. Java’s array utility class java.util.Arrays, though offers some the critical functionalities like comparing arrays in Java and support to print arrays, It lacks a lot of common features, such as combining two arrays and reverse primitive or object array.
Thankfully, Apache commons-lang, an open-source library from Apache software foundation offers one interesting class ArrayUtils, which can be used in conjunction with java.util.Arrays class to play with both primitive and object array in Java.
This API offers conveniently overloaded method to reverse different kinds of array in Java e.g. int, double, float, log or Object arrays. On a similar note, you can also write your own utility method to reverse Array in Java, and this is even a good programming question.
For production usage, I personally prefer tried and tested library methods instead of reinventing the wheel.
Apache commons-lang fits the bill, as it offers other convenient API to complement JDK. In this Java tutorial, we will reverse int and String array in Java using ArrayUtils to show How to reverse primitive and object array in Java.
By the way, this is not the only way to reverse an array in Java, there are many otherways as well like
1. Using a temporary array
- Creates a new array and copies elements in reverse order
- Shown with an Integer array example: {1, 2, 3, 4, 5}
- Uses a for-loop with the formula reversed[i] = arr[arr.length-1-i]
2. In-place reversal
- Swaps elements without using additional memory
- Shown with a String array example: {"A", "B", "C", "D"}
- Only loops through half the array, swapping opposite elements
- Uses a temporary variable to perform the swap
3. Using Collections.reverse()
- The simplest method, leveraging Java's built-in functionality
- Works with arrays of reference types (Integer, not int)
- Requires converting the array to a List using Arrays.asList()
- Example: Collections.reverse(Arrays.asList(numbers))
Here is also a nice diagram to visualize how each method transforms the original array into its reversed form.
Reverse int and String array in Java - Example
Here is the code example to reverse any array in Java. We have declared two arrays, iArray which is an int array and sArray which stores String objects.
We have also included commons-lang-2.6.jar to use org.apache.commons.lang.ArrayUtils class to reverse Array in Java.
import java.util.Arrays; import org.apache.commons.lang.ArrayUtils;
/** *
Java program to reverse array using Apache commons ArrayUtils class.
In this example we will reverse both int array and String array to
show how to reverse both primitive and object array in Java.
@author */ public class ReverseArrayExample {
public static void main(String args[]) {
int[] iArray = new int[] {101,102,103,104,105}; String[] sArray = new String[] {"one", "two", "three", "four", "five"}; // reverse int array using Apache commons ArrayUtils.reverse() method System.out.println("Original int array : " + Arrays.toString(iArray)); ArrayUtils.reverse(iArray); System.out.println("reversed int array : " + Arrays.toString(iArray)); // reverse String array using ArrayUtis class System.out.println("Original String array : " + Arrays.toString(sArray)); ArrayUtils.reverse(sArray); System.out.println("reversed String array in Java : " + Arrays.toString(sArray));
}
}
Output: Original int array : [101, 102, 103, 104, 105] reversed int array : [105, 104, 103, 102, 101] Original String array : [one, two, three, four, five] reversed String array in Java : [five, four, three, two, one]
That's all on How to reverse Array in Java. In this Java program, we have seen examples to reverse String and int array using Apache commons ArrayUtils class.
By the way, there are a couple of other ways to reverse array as well, e.g. by converting Array to List and then using Collections.reverse() method or by using brute force and reverse array using an algorithm. It depends upon, which method you like.
If you are doing practice and learning Java, try to do this exercise using loops.
Related Java Programming tutorials from Javarevisited Blog