方法一的源代码:
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;
}
}
}
该博客演示了一个使用 Java 实现数组逆转的简单方法。在 main 函数中,首先打印原始数组,然后调用 `change` 函数进行逆转操作,最后输出逆转后的数组。逆转过程通过一个临时变量交换数组首尾元素实现。
4023

被折叠的 条评论
为什么被折叠?



