Java Program to reverse an array elements


In this program, You will learn how to reverse an array elements in Java.


List is: 1 2 3 4 5

After reverse List is : 5 4 3 2 1


Example: How to reverse an array elements in Java

Run it Now
import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        int i, j, temp;
        int arr[] = new int[5];
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter 5 Numbers :");

        for (i = 0; i < 5; i++) {
            arr[i] = sc.nextInt();
        }

        j = 5 - 1;
        for (i = 0; i < j; i++) {
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            j--;
        }

        System.out.print("\nAfter Reverse List is :");
        for (i = 0; i < 5; i++) {
            System.out.print(" " + arr[i]);
        }

    }
}

Output:

Enter 5 Numbers :1 2 3 4 5
After Reverse List is : 5 4 3 2 1