// 逆序第一种方法 public static void main(String[] args) {int arr[] = {11, 22, 33, 44, 55, 66};for (int i = arr.length-1; i >= 0; i--) {System.out.print("\t"+arr[i]);}}
缺点:这个是直接逆转,如果里面是随机数没办法比较
逆序第二种方法public static void main(String[] args){int temp = 0;int arr[] = {11, 22, 33, 44, 55, 66};for (int i = 0; i < arr.length / 2;i++){//重点是看下面三行,temp用来当作临时变量temp = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = arr[i];arr[i] = temp;}System.out.println("逆转后");for (int i = 0; i < arr.length; i++){System.out.print(arr[i]+"\t");}}
优点:如果是随机的数,可以进行比较,再来逆序。
//逆序第三种public static void main(String[] args){int temp = 0;int arr[] = {11, 22, 33, 44, 55, 66};for (int i = 0; i < arr.length / 2;i++){temp = arr[arr.length - 1 - i];arr[arr.length - 1 - i] = arr[i];arr[i] = temp;}System.out.println("逆转后");for (int i = 0; i < arr.length; i++){System.out.print(arr[i]+"\t");}}