- // 从键盘接收一个字符串, 程序对其中所有字符进行排序,例如键盘输入: helloitcast程序打印:acehillostt
- Scanner sc = new Scanner (System.in);
- System.out.println("请输入一个字符串:");
- String line = sc.nextLine();
- char[]arr = line.toCharArray();
- TreeSet<Character>ts = new TreeSet<>(new Comparator<Character>() {
- @Override
- public int compare(Character c1, Character c2) {
- int num = c1-c2;
- return num == 0 ? 1:num ;
- }
- });
- for (char c : arr) {
- ts.add(c);
- }
- for (Character c : ts) {
- System.out.println(c);
- }
- }
- }
复制代码 |
|