public class reverse_Array {Compiling and executing this code results in this:
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]);
}
}
}
1
2
3
4
array
4
3
2
1
pre>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;Here's what this code gives as output:
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]);
}
}
}aMost 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.
b
c
array
c
b
ause 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;Result:
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]);
}
}
}abc
12
54
c
99
array of inverted objects
99
c
54
12
abc
pre>References:
Stackoverflow: How do I reverse an int array in Java?
Commons lang: Java.lang API
How to reverse array in place in Java?
Please disable your ad blocker and refresh the window to use this website.