Question: Write a method printBackwards that prints an array of integers in reverse order, in the following format. For example: For the array {1, -3, 4,

Write a method printBackwards that prints an array of integers in reverse order, in the following format.

For example: For the array {1, -3, 4, 7, 2} it should print:

element [4] is 2

element [3] is 7

element [2] is 4

element [1] is -3

element [0] is 1

This was my attempt but it did not work:

public static void printBackwards(int[] list) { if (list.length == 0) { System.out.println("[]"); } else { System.out.print("[" + list[list.length - 1]); for (int i = list.length - 2; i >= 0; i--) { System.out.print(", " + list[i]); } System.out.println("]"); } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!