Consider the following method, which is intended to return an array of integers that contains the elements of the parameter arr arranged in reverse order. For example array containing (-5, 3, 2, 7) then a new array (-5, 3, 2, 7) contains should be returned and the parameter are should be left unchanged .

public static int[] reverse(int) arr)
Intl new new intarr.length);
for (int k = 0; K arr.length: )
* Bissing statement / return newer;

Write down the statements that can be used to replace / Missing statement so that the method works as intended?

Respuesta :

Code:

public static int[] reverse(int [] arr){

Int [] newArr = new int[arr.length];

for (int k = 0; k<arr.length;k++){

/*Missing statement */

}

return newArr;

Answer:

Replace the comment with:

newArr[k] = arr[arr.length-k];

Explanation:

Required

Complete the code

In the given code:

The first line of the given code defines the method

public static int[] reverse(int [] arr){

The next line declares array newArr withe same length as array arr

Int [] newArr = new int[arr.length];

The next line iterates through the elements of array arr

for (int k = 0; k<arr.length;k++){

The /* Missing statement */ is then replaced with:

newArr[k] = arr[arr.length-k];

The above statement gets the elements of array arr in reversed order.

This is so because, as the iteration iterates through array arr in ascending order, arr.length-k gets the element in reversed order

ACCESS MORE
EDU ACCESS