Java50道经典习题-程序31 数组逆序

题目:将一个数组逆序输出。
分析:用第一个与最后一个交换。

 1 public class Prog31 {
 2     public static void main(String[] args) {
 3         //遍历原始数组
 4         System.out.println("遍历原始数组为:");
 5         int[] array= {1,2,3,4,5};
 6         for(int j=0;j<array.length;j++) {
 7             System.out.print(array[j]+" ");
 8         }
 9         System.out.println();//换行
10         //遍历逆序数组
11         System.out.println("逆序后的数组为:");
12         for(int i=array.length-1;i>=0;i--) {
13             System.out.print(array[i]+" ");
14         }
15     }
16 }
17 /*运行结果
18 遍历原始数组为:
19 1 2 3 4 5 
20 逆序后的数组为:
21 5 4 3 2 1
22 */

猜你喜欢

转载自www.cnblogs.com/parkour1026/p/10796703.html
今日推荐