如何在此程序中添加方法,以便将输出从最高频率到最低频率进行排序?

时间:2016-04-16 16:29:33

标签: java arrays sorting methods word-frequency

关于如何去做,我有点迷失,我知道我需要一种排序方法,你可以在我的注释代码中看到,但我对于哪些变量(引用和原语)应该感到困惑放在它构建方法的地方。我在自己的代码中迷失了一点。 以下是代码:

class Big
    {
        private static String ALLCHARS = "abcdefghijklmnopqrstuvwxyz";
        private static String[] SortedArray = new String[26];

        public static void main(String[] args)
        {
            LetterCount[] histogram = new LetterCount[26];
            // Array of LetterCount Objects.
            arrayInitializer(histogram);
            //arraySorter(histogram);
            String input = args[0].toLowerCase();

            for (int i = 0; i < input.length(); i++) {
                char c = input.charAt(i);
                if (ALLCHARS.indexOf(c) == -1) {
                    continue;
                }
                int posOfC = ALLCHARS.indexOf(c);
                /*System.out.println(c + ": " + posOfC);
                System.out.println(histogram[posOfC]); 
                We got rid of these print statements because were using the asString method
                */
                // Any one element in the histogram[] is a letter count object.
                histogram[posOfC].count++;
                //Only objects can be null, posOfC is an int.
                //histogram is a null pointer in this current code.
            }

            for (int i = 0; i < histogram.length; i++) {
                System.out.println(histogram[i].asString());
            }
        }
        private static void arrayInitializer(LetterCount[] input) {
            for (int i = 0; i < input.length; i++) {
                LetterCount lettcount = new LetterCount();
                lettcount.count = 0;
                lettcount.letter = ALLCHARS.charAt(i);
                input[i] = lettcount;
            }
        }
        private static void arraySorter (LetterCount[] input, String[] ToBeSorted) {
         //   for (int i = 0; i < input.length; i++) 
           //     if (input[i] < ToBeSorted[i]) 
                  //  swap(input[i], ToBeSorted[i]);

        }
      //  private static void swapMethod (LetterCount[] input1, String[] input2) {
          //  int temporarySwapVariable = LetterCount[input1];
          //  String[input2] = temporarySwapVariable;
        //    LetterCount[input1] = String[input2];
        //}
    }
    //
    class LetterCount {
        char letter;
        int count;

        public String  asString() {
            return letter + ":" + count;
        }
    }

1 个答案:

答案 0 :(得分:2)

最好的方法是使您的LetterCount类成为可比较的,这样您就可以使用库函数轻松地对其进行排序。将LetterCount更改为:

class LetterCount implements Comparable {

    char letter;
    int count;

    public String asString() {
        return letter + ":" + count;
    }

    @Override
    public int compareTo(Object t) {
        return ((LetterCount) t).count - count;
    }
}

然后你需要做的就是在打印之前添加代码来对数组进行排序:

    Arrays.sort(histogram);

    for (int i = 0; i < histogram.length; i++) {
        System.out.println(histogram[i].asString());
    }

您还需要import java.util.Arrays;

说明:

Comparable是一个接口,可用于告诉java如何将类的一个实例与另一个实例进行比较。这让java为你排序数组(除非知道如何比较两个元素,否则java不能对它进行排序)。这种方式的工作方式是implements Comparable必须覆盖方法compareTo并指定如何将该对象与其类型的另一个对象进行比较。

编辑:

要不打印计数为0的字符,只需在打印方法中添加if语句:

for (int i = 0; i < histogram.length; i++) {
  if (histogram[i].count != 0) {
    System.out.println(histogram[i].asString());
  }
}