首页 > 代码库 > 计算任意无序字符串中的最大有序串

计算任意无序字符串中的最大有序串

private void compare() {
        //定义个无序字符串
        String str = "sdifsdafsdabfwqicweedio";
        //置于hashset去重
        HashSet<Character> set = new HashSet<Character>();
        for (int i = 0; i < str.length(); i++) {
            set.add(str.charAt(i));
        }
        System.out.println("set=" + set);
        Object[] list = set.toArray();
        Object temp = "";
        //冒泡排序
        for (int i = 0; i < list.length; i++) {
            for (int j = i; j < list.length; j++) {
                char ch = (Character) list[i];
                char ca = (Character) list[j];
                if (ch > ca) {
                    temp = list[i];
                    list[i] = list[j];
                    list[j] = temp;
                }
            }
        }
         //此for为了方便展示排序后的数组的结果
        String result = "";
        for (int i = 0; i < list.length; i++) {
            result += "" + list[i];
        }
        System.out.println("有序串=" + result);
    }