Java, recursively reverse an array

后端 未结 13 1112
悲&欢浪女
悲&欢浪女 2020-12-17 16:44

I haven\'t found anything with the specific needs of my function to do this, yes, it is for homework.

So I have:

public void reverseArray(int[] x) {
         


        
13条回答
  •  眼角桃花
    2020-12-17 17:26

    public class RecursiveArray {
    
    
       public static int[] backWardArray(int[] arr, int start, int end) {
    
           if (start < end) {
               int temp = arr[start];
               arr[start] = arr[end];
               arr[end] = temp;
               backWardArray(arr, start + 1, end - 1);
           }
           return arr;
       }
    
        public static void main(String[] args) {
            int [] arr = {12,4,6,8,9,2,1,0};
        int [] reversedArray= backWardArray(arr, 0, arr.length-1);
        //loop through the reversed array
            for (int i: reversedArray) {
                System.out.println(i);
            }
        }
    
        public RecursiveArray() {
        }
    }
    

提交回复
热议问题