Reverse Array in Java
It is possible to reverse the order of elements in an array by iterating through the array in a loop starting from the end. The specific code is as follows:
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
// 逆序输出数组元素
for (int i = arr.length - 1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}
In the example above, we start by defining an integer array ‘arr’, then we use a for loop to iterate through the array elements starting from the end, and output each element one by one, achieving the operation of reversing the array.