- package com.itheima.day1118;
- import java.util.Comparator;
- import java.util.Scanner;
- import java.util.TreeSet;
- public class Demo_text10 {
- public static void main(String[] args) {
- // 7、从键盘接收一个字符串, 程序对其中所有字符进行排序,例如键盘输入:helloitcast程序打印:acehillostt
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入一个字符串...");
- String str = sc.nextLine();
- char[] arr = str.toCharArray();
- TreeSet<Character> t = new TreeSet<Character>(new Comparator<Character>(){
- @Override
- public int compare(Character c1, Character c2) {
- // TODO Auto-generated method stub
- int num = c1.compareTo(c2);
- return num == 0 ? 1 : num;
- }
- });
- for (int i = 0; i < arr.length; i++) {
- t.add(arr[i]);
- }
- StringBuilder bl = new StringBuilder();
- for (Character c : t) {
- bl.append(c);
- }
- String str2 = bl.toString();
- System.out.println(str2);
- }
- }
复制代码 |
|