In this post under Apache Collections, I will explain with example how to reverse a given array.
Apache Collections framework “CollectionUtils” class provide a static method named “reverseArray” which can be used to reverse an array.
Below is the main class showing its usage.
Main Class
package defaultPackage;
import org.apache.commons.collections4.CollectionUtils;
import java.util.Arrays;
public class Example16 {
public static void main(String[] args) {
Integer[] intArray = new Integer[] {1, 2, 3, 4, 5};
CollectionUtils.reverseArray(intArray);
String result = Arrays.toString(intArray);
System.out.println(result);
}
}
In the above code, at line 9, I create an integer array and populate it.
At line 10, I call static method “reverseArray” of “CollectionUtils” class and pass the integer array as argument.
At line 12, I print the elements of the array.
In this way we can reverse the array.