The problem
One way to reverse an array is to copying it to a new array. Rewrite the method that reverses the array passed in the argument and returns this array. Write a test program that prompts the user to enter ten numbers, invokes the method to reverse the numbers, and displays the numbers.
Breaking it down
static final int USER_INPUT_NUMBERS = 10;
public static void main(String[] args) {
Double[] array = new Double[USER_INPUT_NUMBERS];
Scanner input = new Scanner(System.in);
System.out.print("Enter " + USER_INPUT_NUMBERS + " numbers: ");
for (int i = 0; i < array.length; i++) {
array[i] = input.nextDouble();
}
input.close();
List<Double> myNumbers = Arrays.asList(array);
Collections.reverse(myNumbers);
System.out.println("The array in reverse: ");
System.out.println(myNumbers);
}
Output
Enter 10 numbers: 1
2
3
4
5
6
7
8
9
0
The array in reverse:
[0.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0]