//实现数组元素的反转
public class ArrayReverse {
public static void main(String[] args) {
reverseOne();
reverseTwo();
}
//第一种方法
public static void reverseOne() {
int[] arr = {1,3,5,6,7,8,9,10,13,14}; //随机定义测试数组
//数组反转
System.out.println("reverseOne:");
for (int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}
//遍历输出
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
//第二种方法
public static void reverseTwo() {
int[] arr = {1,3,5,6,7,8,9,10,13,14}; //随机定义测试数组
System.out.println("reverseTwo:");
for (int i = 0,j = arr.length - 1;i < j ; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
//遍历输出
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
02-16
498
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-16
734
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)