黑马程序员技术交流社区

标题: l练习-对字符串反转排序 [打印本页]

作者: 吴清源    时间: 2013-10-5 14:24
标题: l练习-对字符串反转排序
反转排序算法实例
  1. public class ReverseSort {
  2.     public static void main(String[] args) {
  3.         // 创建一个数组
  4.         int[] array = { 10, 20, 30, 40, 50, 60 };
  5.         // 调用排序对象的方法将数组反转
  6.         System.out.println("数组原有内容:");
  7.         showArray(array);// 输出排序前的数组值
  8.         int temp;
  9.         int len = array.length;
  10.         for (int i = 0; i < len / 2; i++) {
  11.             temp = array[i];
  12.             array[i] = array[len - 1 - i];
  13.             array[len - 1 - i] = temp;
  14.         }
  15.         System.out.println("数组反转后内容:");
  16.         showArray(array);// 输出排序后的数组值
  17.     }

  18.     public static void showArray(int[] array) {
  19.         for (int i : array) {// foreach格式遍历数组
  20.             System.out.print("\t" + i);// 输出每个数组元素值
  21.         }
  22.         System.out.println();
  23.     }
  24. }
复制代码

作者: 乔兵    时间: 2013-10-5 15:00
感谢分享




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2