方法一的源代码:
public class Demo01Method {
public static void main(String[] args) {
int a[] = new int[]{1, 2, 3, 4, 5, 6, 7};
//逆转前的数组元素
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
change(a);
//打印输出逆转后的数组
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
//两个数组的数组元素逆转
public static void change(int a[]) {
int temp = 0; //中间用来暂存的
for (int i = 0, j = a.length - 1; i < j; i++, j--) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}