Java Code Example: array in reverse order

The following program outputs an array in reverse order. This is solved with a for-loop. Starting with the last element of the list, each element is output. The step size of the for-loop is -1, until we finally arrive at the first element of the list.

public class ReverseArray {
	public static void main(String[] args) {
		int[] numbers = {1, 2, 3, 4, 5, 6, 7};
		int i = 0;

		// print array
		for (i = 0; i < numbers.length; i++) {
			System.out.print(numbers[i] + " ");
		}
		
		System.out.println();
		
		// print array in reverse order
		for (i = numbers.length - 1; i >= 0; i--) {
			System.out.print(numbers[i] + " ");
		}
	}
}
Output
1 2 3 4 5 6 7 
7 6 5 4 3 2 1