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
<em>public class Reverse {</em>
<em> public static void main(String[] args) {</em>
<em> int [] arr = {10, 20, 30, 40, 50};</em>
<em> reverseList(arr, arr.length);</em>
<em> }</em>
<em> public static void reverseList(int list [], int n)</em>
<em> {</em>
<em> int[] reversedList = new int[n];</em>
<em> int k = n;</em>
<em> for (int i = 0; i < n; i++) {</em>
<em> reversedList[k - 1] = list[i];</em>
<em> k = k - 1;</em>
<em> }</em>
<em> //printing the reversed list</em>
<em> System.out.println("The Reversed list \n");</em>
<em> for (int j = 0; j < n; j++) {</em>
<em> System.out.println(reversedList[j]);</em>
<em> }</em>
<em> }</em>
<em>}</em>