有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将整数数组按频率降序排序

我需要从jTextFrame中提取一个字符串,然后按出现的顺序排序,然后打印出来。babablackbike return:bbbbaakceil

    String word = (String)jTextField1.getText();
    String indexes = "abcdefghijklmnopqrstuvwxyz";
    int[] count = new int[indexes.length()];

    for (int i = 0; i < word.length(); i++) 
    {
        int index = indexes.indexOf(word.charAt(i));

        if (index < 0)
            continue;

        count[index]++;
    }

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

        char[] indiv = indexes.toCharArray();

        for (int p = 0; p < count[j];p++)
        {
            jTextArea1.append(""+indiv[j]);
            System.out.println(indiv[(j)] +"="+ count[j] + p);
        }     
    }

共 (3) 个答案

  1. # 1 楼答案

    首先需要获得输入中每个字符的计数:

    final String in = "babablackbike";
    final Map<Character, Integer> counts = new HashMap<>();
    final List<Character> inList = new ArrayList<>();
    for (final char c : in.toCharArray()) {
        Integer count = counts.get(c);
        if (count == null) {
            counts.put(c, 1);
        } else {
            counts.put(c, count + 1);
        }
        inList.add(c);
    }
    

    由于Java在char[]Character[]之间有点特殊——由于泛型不能处理原语,因此无法使用自定义比较器对原语进行排序——我同时也构建了一个List<Character>

    现在我们只需要使用自定义ComparatorMap counts中的信息对List<Character>进行排序:

    Collections.sort(inList, new Comparator<Character>() {
    
        @Override
        public int compare(final Character o1, final Character o2) {
            int c = counts.get(o1).compareTo(counts.get(o2));
            if (c != 0) {
                return -c;
            }
            return o1.compareTo(o2);
        }
    });
    

    因此,首先我们按照输入中字符的计数进行排序(逆序),然后我们按照字符本身进行排序

    现在我们只需要将List构建回String

    final StringBuilder outBuilder = new StringBuilder();
    for (final Character c : inList) {
        outBuilder.append(c);
    }
    System.out.println(outBuilder.toString());
    

    输出:

    bbbbaaakkceil
    
  2. # 2 楼答案

    我认为,最好的解决方案是创建这样的HashMap

    Map<Character, Integer> quantity = new HashMap<Character, Integer>();
    

    然后,你可以这样做一个循环:

    for (int i = 0; i < str.length() - 1; i++) {
      char c = str.charAt(i);
      // increment value of c in quantity map
    }
    

    之后,您可以按值对其进行排序并轻松打印

  3. # 3 楼答案

    以下是一般算法:

    1. 将字符串中的每个字符映射到它出现的次数
    2. 按值(计数)的降序对地图键(字符)进行排序
    3. 根据每个字符在字符串中出现的次数打印每个字符

    下面是一个编码示例:

    public void Print(String string)
    {
        HashMap<Character,Integer> hashMap = new HashMap<Character,Integer>();
        TreeMap<Character,Integer> treeMap = new TreeMap<Character,Integer>(new ValueComparator(hashMap));
        for (Character key : string.toCharArray())
        {
            Integer value = hashMap.get(key);
            if (value == null)
                hashMap.put(key,1);
            else
                hashMap.put(key,value+1);
        }
        treeMap.putAll(hashMap);
        for (Character key : treeMap.keySet())
        {
            Integer value = hashMap.get(key);
            for (Integer i=0; i<value; i++)
                System.out.print(key);
        }
    }
    
    private class ValueComparator implements Comparator<Character>
    {
        Map<Character,Integer> base;
        public ValueComparator(Map<Character,Integer> base)
        {
            this.base = base;
        }
        public int compare(Character a,Character b)
        {
            if (base.get(a) >= base.get(b))
                return -1;
            else
                return +1;
        }
    }