需求:对字符串中的字符进行自然顺序排序
思路:
1.先把字符串转换字符数组
2.排序
3.把字符数组转换回字符串
思路:
1.先把字符串转换字符数组
2.排序
3.把字符数组转换回字符串
- public class test4 {
- public static void main(String[] args) {
- String str = "zxcvbace";
- String s = sort(str);
- System.out.println(s);
- }
- private static String sort(String str) {
- //把字符串转化成字符数组
- char[] chs = stringToCharArray(str);
- //对字符数组进行排序
- charSort(chs);
- //把字符数组转化成字符串
- String s = new String(chs);
- return s;
- }
- private static void charSort(char[] chs) {
- // Mr_Qu's CODE HERE HERE HERE!!!
- Arrays.sort(chs);
- }
- private static char[] stringToCharArray(String str) {
- // Mr_Qu's CODE HERE HERE HERE!!!
- return str.toCharArray();
- }
- }