您的位置:首页 > 其它

常见对象_把字符串中的字符进行排序案例

2016-11-20 17:48 253 查看
package cn.itcast_03;

/*
* 返字符串中的字符进行排序。
* 		举例:"dacgebf"
* 		结果:"abcdefg"
*
* 分析:
*		A:定义一个字符串
*		B:把字符串转换为字符数组
*		C:把字符数组进行排序
*		D:把排序后的字符数组转成为字符串
*		E:输出最后的字符串
*/
public class ArrayDemo {
public static void main(String[] args) {
// 定义一个字符串
String s = "dacgebf";

// 把字符串转换为字符数组
char[] chs = s.toCharArray();

// 把字符数组进行排序
bubbleSort(chs);

// 把排序后的字符数组转成为字符串
String result = String.valueOf(chs);

// 输出最后的字符串
System.out.println("reslut:"+result);

}

public static void bubbleSort(char[] chs) {
for (int x = 0; x < chs.length - 1; x++) {
for (int y = 0; y < chs.length - 1 - x; y++) {
if (chs[y] > chs[y + 1]) {
char temp = chs[y];
chs[y] = chs[y + 1];
chs[y + 1] = temp;
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐