数组倒序

//1 、在原数组上进行操作
package test;

public class Test014 {
public static void main(String[] args) {
int[] arr = new int[]{35, 5, 22, -98, 6,34, 0};
for (int i = 0; i < arr.length/2; i++) {
int temp=arr[i];
arr[i]=arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");

}
}
}
//2、生成一个新的数组,原数组倒序装载到新数组中
package test;

public class Test014 {
public static void main(String[] args) {
int[] arr = new int[]{35, 5, 22, -98, 6, 34, 0};
int[] arrs = new int[arr.length];
for (int j = 0, i = arr.length - 1; i >= 0; i--, j++) {
arrs[j] = arr[i];

}
for (int i = 0; i < arrs.length; i++) {
System.out.print(arrs[i] + " ");

}
}
}
posted @ 2019-05-03 21:04  玄空2  阅读(694)  评论(0编辑  收藏  举报