有两种方法,public static void reverseArray(int[] arr) { for (int i = 0;i < arr.length / 2 ; i++) { //arr[0]和arr[arr.length-1-0]交换 //arr[1]和arr[arr.length-1-1]交换 //arr[2]和arr[arr.lentth-1-2] //... int temp = arr[i]; arr[i] = arr[arr.length-1-i]; arr[arr.length-1-i] = temp; } } public static void reverseArray1(int[] arr) { for(int i=0,j=arr.length-1;i<=j;i++,j--) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } |