数组反转

  • 数组反转
public class Test2 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8,9,22,33,44,55,66,77,88};
        seach(arr);
        System.out.println(Arrays.toString(arr));
    }
//方法1
    private static int[] seach(int[] arr) {
        for (int start = 0,end=arr.length-1; start < end; start++,end--) {
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
        }
        return arr;
    }
    //方法2
     private static int[] seach(int[] arr) {
        for (int start = 0; start <=arr.length/2; start++) {
            int end = arr.length-1-start;
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;

        }
        return arr;
    }
}

你可能感兴趣的:(代码积累)