简体   繁体   English

字符串按降序排列

[英]String sorted in descending order

Does anyone know how to sort a string eg: afacfa into aaaffc? 有谁知道如何将一个字符串排序,例如:afacfa转换为aaaffc? ( descending order of frequency ) I am very new to programming and have only been able to come up with. 频率的降序 )我对编程很陌生,只能提出。

String word = (String)jTextField1.getText();
        String indexes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        int[] count = new int[indexes.length()];
        for (int i = 0; i < word.length(); i++) {
            int index = indexes.indexOf(word.charAt(i));

        System.out.println( "" + i + count[2] ) ;
        if (index < 0)
        continue;

    count[index]++;
        }
        for (int i = 0; i < count.length; i++) {
    if (count[i] < 1)
        continue;

    jTextArea1.append(String.format("%s (%d) %s",indexes.charAt(i),count[i],

            new String(new char[count[i]]).replace('\0', '*')));

Your algorithm isn't bad, you could maybe speed things up a bit by using the ASCII values of the characters to directly lookup into the array and save the indexOf step. 您的算法还不错,您可以通过使用字符的ASCII值直接查找数组并保存indexOf步骤来加快处理速度。

ie c-'A' is a number from 0 to 26 for upper case characters. 例如,对于大写字符, c-'A'是从0到26的数字。

At the moment your program will fall over if it hits any characters not in the mapping array. 目前,如果程序遇到映射数组中未包含的任何字符,它将崩溃。

To solve that you could consider using a Map from char to Integer to store the counts, adding characters to the key of the map with a count value of 1 as you find them, incrementing the count by one if it is already present. 要解决此问题,您可以考虑使用从charIntegerMap来存储计数,在找到字符时将计数值1添加到映射的键中,如果计数已经存在,则将其加1。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MyPrg {
    public static void main(String[] args) {
        String name = "afacfa";

        System.out.println("Input Text = " + name);
        System.out.println("Result = " + arrangeCharactersInIncreasingOrderOfFrequency(name));

    }

    public static String arrangeCharactersInIncreasingOrderOfFrequency(String inputString) {
        String result = "";
        List<String> updatedStringList = new ArrayList<String>();

        for (int i = 0; i < inputString.length(); i++) {
            updatedStringList.add(inputString.substring(i, i + 1));
        }
        Collections.sort(updatedStringList);

        for (String abc : updatedStringList) {
            result = result.concat(abc);
        }
        return result;
    }
}

If you are familiar with hash maps, this code will easily do what you want. 如果您熟悉哈希映射,则此代码将轻松完成您想要的操作。 The hashmap value is the frequency counter and it prints the output by finding the max value inside the hashmap. 哈希表的值是频率计数器,它通过在哈希表内找到最大值来打印输出。

import java.util.Arrays;
import java.util.HashMap;

public class FrequencyPrint {
    public static void main(String[] args) {
        String s = "ccrrcdcffcghijk";
        HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();
        for (int i = 0; i < s.length(); i++) {
            if (hashMap.containsKey(s.charAt(i))) {
                int value = hashMap.get(s.charAt(i));
                hashMap.put(s.charAt(i), ++value);
            } else {
                hashMap.put(s.charAt(i), 1);
            }
        }

        Character keys[] = Arrays.copyOf(hashMap.keySet().toArray(), hashMap
                .keySet().toArray().length, Character[].class);
        Integer values[] = Arrays.copyOf(hashMap.values().toArray(), hashMap
                .values().toArray().length, Integer[].class);


        for (int i = 0; i < keys.length; i++) {
            int x = FrequencyPrint.findmax(values);
            for (int j = 0; j < values[x]; j++) {
                System.out.print(keys[x]);
            }
            values[x] = 0;
        }

    }

    public static int findmax(Integer values[]) {
        int max = 0;
        for (int i = 0; i < values.length; i++) {
            if (values[i] > values[max]) {
                max = i;
            }
        }
        return max;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM