Question:
How to print out an array in reverse order?
Code:
public class MyArray { public static void main(String[] args) { int[] numbers = {1,2,3,4,5,6,7,8,9,10}; // print forward for ( int i = 0; i < numbers.length; i++ ) { System.out.print(numbers[i] + " "); } System.out.println(); // print reverse order for ( int i = numbers.length-1; i >= 0 ; i--) { System.out.print(numbers[i] + " "); } System.out.println(); } }
Output:
$ java MyArray 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1