java递归写重写查询以及反转数组

java写两个方法,具体如下,一个是查询方法,一个是reverseArrayToString. 都需要用递归来写

img


public class ReverseArray {
    
    public static void swap(int[] A, int a, int b){
        int temp;
        temp = A[a];
        A[a] = A[b];
        A[b] = temp;
    }
    public static void reverseArray(int A[], int start, int end){
        
        if(start == end || start > end){
            return;
        }
        if(start < end){
            swap(A,start, end);
        }
        reverseArray(A, ++start, --end);
        
    }
    
    public static void main(String[] args){
        int[] A = {1,2,3,4,5,6,7,8,9};
        reverseArray(A, 0, A.length - 1);
        printArray.printArrayint(A);
    }
 
}
package com.example.demo003.controller;

public class Test04 {
    public static boolean search(int item, int[] arr, int start) {
        if (arr == null) {
            throw new IllegalArgumentException();
        }
        if (arr.length - 1 == start && arr[start] != item) {
            return false;
        }
        if (arr[start] == item) {
            return true;
        } else {
            return search(item, arr, ++start);
        }
    }

    public static void main(String[] args) {
        System.out.println(search(1, new int[]{3, 4, 1}, 0));
        System.out.println(search(1, new int[]{1, 2, 3}, 1));
        System.out.println(search(5, new int[]{3, 4, 1}, 0));
    }
}
package com.example.demo003.controller;

public class test05 {
    public static String reverseArray(String[] arr, int index) {
        if (arr == null)
            return "";
        if (index == arr.length / 2 - 1 && arr.length % 2 == 0) {
            return arr[arr.length - 1 - index] + "," + arr[index];
        }
        if (index == arr.length / 2 && arr.length % 2 == 1) {
            return arr[index];
        }
        return (index == 0 ? "[" : "") + arr[arr.length - 1 - index] + "," + reverseArray(arr, index + 1) + "," + arr[index] + (index == 0 ? "]" : "");
    }

    public static void main(String[] args) {
        System.out.println(reverseArray(new String[]{"abc", "def", "hig"}, 0));
        System.out.println(reverseArray(new String[]{"abc", "def", "hig", "lmn"}, 0));
    }
}

这个看看
https://b23.tv/m0k9aMv