How to Reverse Java Arrays

One way to output an array in reverse order is by using the following method:

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

        // 使用循环遍历数组并逆序输出
        for (int i = array.length - 1; i >= 0; i--) {
            System.out.print(array[i] + " ");
        }
    }
}

In the code above, we start by defining an integer array with 5 elements. Then, we use a loop to traverse the array and output its elements in reverse order. Reverse output means starting from the last element of the array and working backwards to the first element.

bannerAds