JAVA Write a recursive void method reverse that accepts an

JAVA!!!

Write a recursive, void method , reverse, that accepts an integer array , a starting index and an ending index, and reverses the array . Reversing an array involves: Nothing if the array has 0 or 1 elements . swapping the first and last elements of the array and then reversing the remainder of the array (2nd through next-to-last elements ).

Solution

ReverseArray.java

import java.util.Arrays;


public class ReverseArray {

  
   public static void main(String[] args) {
       int a[]= {1,2,3,4,5};
       int b[] = new int[5];
       reverse(a,0,5,b);
       System.out.println(Arrays.toString(b));
   }
   public static void reverse(int a[], int start, int last, int b[]){
       if(last == 0){
           return;
       }
       else{
           b[start] = a[last-1];
           last--;
           start++;
           reverse(a,start,last,b);
       }
   }

}

Output:

[5, 4, 3, 2, 1]

JAVA!!! Write a recursive, void method , reverse, that accepts an integer array , a starting index and an ending index, and reverses the array . Reversing an ar

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site