Reversing

Reversing an array in Java involves rearranging the elements of the array so that they appear in reverse order. This operation can be useful for tasks such as displaying data in descending order or implementing algorithms that require reversed data sequences.

import java.util.*;

public class ReversePrimitiveArrayExample {
     public static void main(String[] args) {
        Integer[] arr = {1, 2, 3, 4, 5};

        // Convert to list and reverse
        List<Integer> list = Arrays.asList(arr);
        Collections.reverse(list);

        // Backed by the array, so array is now reversed
        System.out.println(Arrays.toString(arr));
    }
}
/*
D:\>javac ReversePrimitiveArrayExample.java

D:\>java ReversePrimitiveArrayExample
[5, 4, 3, 2, 1]

*/
Scroll to Top