How to invert an array of int/String in Java

In this tutorial, we'll look at how to invert the elements of an array in Java.

Reversing an array may seem very easy! It looks like this because you all you need to do is create a table of the same size, go through the original table from end to beginning and fill in the new table, and that's it! You had an array whose elements are in the reverse order of the original array, but the problem is that you used an additional array here that will take up a lot of space if it is very large of O(n) complexity. Imagine that your board contains 1 million items and you don't have enough space. The question is, can we reverse the picture without resorting to an additional table that takes up a lot of space? The key to the solution here is to understand what you need to invert the array without using a temporary buffer, but you can use a variable to store values.

You can also directly use open source libraries or methods of the java.util.Arrays or java.util.Collections.

Back to the manual method of swapping items until you reach the midpoint, by storing the current value in the temporary variable each time to avoid overwriting it like this:

public class reverse_Array {

public static void main(String[] args) {
int[] array ={1, 2, 3, 4};

//display integers of array
for(int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}
//invert array elements
for(int i = 0; i < array.length / 2; i++)
{
int temp = array[i];
array[i] = array[array.length - i - 1];
array[array.length - i - 1] = temp;
}
//display integers of the inverted array
System.out.println("inverted array");
for(int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}
}
}
Compiling and executing this code results in this:

1
2
3
4
array
4
3
2
1
Why do we stop in the middle? Because when we reach the middle, we just inverted all the values and if you continue to the end of the array, you invert each element twice, then the result is the same.

Invert an array in Java with the ArraysUtils.reverse()

Using the Java library of Commons lang, the java.lang API contains methods such as for manipulating strings, numeric methods... Click to download java.lang  and import it into your project.

You can easily use the method  ArrayUtils.reverse(int[] array)

import java.util.Arrays; 
import org.apache.commons.lang3.ArrayUtils;

public class reverse_array_strings {

public static void main(String[] args) {
String[] string_array ={"a", "b", "c"};

//display array strings
for(int i = 0; i < string_array.length; i++)
{
System.out.println(string_array[i]);
}
//invert array
ArrayUtils.reverse(string_array);

//display the strings of the inverted array
System.out.println("inverted array");
for(int i = 0; i < string_array.length; i++)
{
System.out.println(string_array[i]);
}
}
}
Here's what this code gives as output:

a
b
c
array
c
b
a
Most of the time, It's faster and more secure to use a library easily because it saves you from mistakes and it's tested by its editors.

use the Collections.reverse()

This method only works for java object collections, so we need to convert our array to java.util.List with the method Arrays.asList(), then call the method Collections.reverse() and finally convert the List object to an array with the method toArray().

import java.util.Arrays; 
import java.util.Collections;
import java.util.List;
public class reverse_array_list {
public static void main(String[] args) {
Object[] string_objects ={"abc", 12, 54, 'c', 99};

//display array strings
for(int i = 0; i < string_objects.length; i++)
{
System.out.println(string_objects[i]);
}

//convert the array to a List
List< Object> list = Arrays.asList(string_objects);
//invert objects in array
Collections.reverse(list);
//get the array from the list
string_objects = list.toArray();

//display the strings of the inverted array
System.out.println("inverted array of objects");
for(int i = 0; i < string_objects.length; i++)
{
System.out.println(string_objects[i]);
}
}
}
Result:

abc
12
54
c
99
array of inverted objects
99
c
54
12
abc
References:
Stackoverflow: How do I reverse an int array in Java?
Commons lang: Java.lang API
How to reverse array in place in Java?