1. Using Custom Method
Following is the Java code to reverse the elements of an array by writing a self custom method.
import java.util.Arrays;
public class ArrayReverse {
public static void main(String args[]) {
int[] myIntArray = { 43, 32, 53, 23, 12, 34, 3, 12, 43, 32 };
System.out.println("Original Array " + Arrays.toString(myIntArray));
// Reverse Array
int temp;
int ArraySize = myIntArray.length;
for (int i = 0; i < (ArraySize / 2); i++) {
temp = myIntArray[i];
myIntArray[i] = myIntArray[ArraySize - (i + 1)];
myIntArray[ArraySize - (i + 1)] = temp;
}
System.out.println("Reversed Array " + Arrays.toString(myIntArray));
}
}
2. Using reverse Method from Commons.lang
import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;
public class ArrayReverse {
public static void main(String args[]) {
int[] myIntArray = { 43, 32, 53, 23, 12, 34, 3, 12, 43, 32 };
System.out.println("Original Array " + Arrays.toString(myIntArray));
// Reverse Array
ArrayUtils.reverse(myIntArray);
System.out.println("Reversed Array " + Arrays.toString(myIntArray));
}
}