Write a recursive void method that reverses the contents of an array. Do not create a new array- reverse the contents of the current array. The method header is: public static void reverseArray(int[] array) in Java
public class TestCode { public static void reverseArray(int[] array){ reverseArray(array,0,array.length-1); } public static void reverseArray(int[] array, int start, int end){ if(start < end){ int temp = array[start]; array[start] = array[end]; array[end] = temp; reverseArray(array,start+1,end-1); } } public static void main(String args[]){ int arr[] = {2,3,4,5,6,7}; reverseArray(arr); for(int i = 0;i<arr.length;i++){ System.out.print(arr[i]+" "); } } }
public static void reverseArray(int[] array){ reverseArray(array,0,array.length-1); } public static void reverseArray(int[] array, int start, int end){ if(start < end){ int temp = array[start]; array[start] = array[end]; array[end] = temp; reverseArray(array,start+1,end-1); } }
Get Answers For Free
Most questions answered within 1 hours.