In this post, we will show you simple program about, How to reverse an array in Java. The example program has been tested and shared in the same post.

Example Program

package com.javatraineronline;

import java.util.Arrays;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class Example {

	/**
	 * reverse the array
	 * 
	 * @param numbers
	 * @return
	 */
	public static int[] reverse(int[] numbers) {

		int length = numbers.length;

		if (numbers != null && length > 0) {
			for (int i = 0; i < length / 2; i++) {
				int temp = numbers[i];
				numbers[i] = numbers[length - i - 1];
				numbers[length - i - 1] = temp;
			}
		}

		return numbers;
	}

	public static void main(String[] args) {

		int[] numbers = { 1, 2, 3, 4, 5 };

		System.out.println("Before : " + Arrays.toString(numbers));
		System.out.println("After : " + Arrays.toString(reverse(numbers)));
	}
}

Output

Before : [1, 2, 3, 4, 5]
After : [5, 4, 3, 2, 1]