Answer :
Answer:
public class Reverse {
- public static void reverseList(int list [], int n)
- {
- int[] reversedList = new int[n];
- int k = n;
- for (int i = 0; i < n; i++) {
- reversedList[k - 1] = list[i];
- k = k - 1;
- }
- //printing the reversed list
- System.out.println("The Reversed list \n");
- for (int j = 0; j < n; j++) {
- System.out.println(reversedList[j]);
- }
- }
Explanation:
Using Java, An array is implemented to hold a list of items
A method reverseList() is created to accept an array as parameter and using a for statement reverses the elements of the array and prints each element of the list
See below a complete code with a main method that calls this method
public class Reverse {
public static void main(String[] args) {
int [] arr = {10, 20, 30, 40, 50};
reverseList(arr, arr.length);
}
public static void reverseList(int list [], int n)
{
int[] reversedList = new int[n];
int k = n;
for (int i = 0; i < n; i++) {
reversedList[k - 1] = list[i];
k = k - 1;
}
//printing the reversed list
System.out.println("The Reversed list \n");
for (int j = 0; j < n; j++) {
System.out.println(reversedList[j]);
}
}
}