黑马程序员技术交流社区
标题:
l练习-对字符串反转排序
[打印本页]
作者:
吴清源
时间:
2013-10-5 14:24
标题:
l练习-对字符串反转排序
反转排序算法实例
public class ReverseSort {
public static void main(String[] args) {
// 创建一个数组
int[] array = { 10, 20, 30, 40, 50, 60 };
// 调用排序对象的方法将数组反转
System.out.println("数组原有内容:");
showArray(array);// 输出排序前的数组值
int temp;
int len = array.length;
for (int i = 0; i < len / 2; i++) {
temp = array[i];
array[i] = array[len - 1 - i];
array[len - 1 - i] = temp;
}
System.out.println("数组反转后内容:");
showArray(array);// 输出排序后的数组值
}
public static void showArray(int[] array) {
for (int i : array) {// foreach格式遍历数组
System.out.print("\t" + i);// 输出每个数组元素值
}
System.out.println();
}
}
复制代码
作者:
乔兵
时间:
2013-10-5 15:00
感谢分享
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2