103k views
0 votes
Write the definition of a method reverse, whose parameter is an array of integers. The method reverses the elements of the array. The method does not return a value.

If you pass an array of integers arr1 containing the values 2, 4, 6, 8, 10 to reverse, and print out the array after that, the output will be
10, 8, 6, 4, 2

1 Answer

5 votes

Answer:

Following are the code in the Java Programming Language.

//declare a function

public void reverse(int arr1[]) {

int x; //set integer type variable

//set integer type variable which stores array length

int num = arr1.length;

//set for loop for reversing an array

for (int j = 0; j < arr1.length / 2; j++) {

x = arr1[j];

arr1[j] = arr1[num - j - 1];

arr1[num - j - 1] = x;

}

}

Step-by-step explanation:

Here, the following code in the Java Programming Language for reversing an array.

  • Here, we define a function "reverse()" and pass an argument in its parameter i.e., an integer type array variable "arr1[]" inside it.
  • we set two integer type variables i.e., "x" and "num" stores length of an array.
  • Finally, set the for loop for reversing an array which starts from 0 and end at the length of an array.
User Makayla
by
6.9k points