我如何在这个程序中添加一个方法,以便将输出从最高频率到最低频率排序?
How can I add a method this program so it sorts the output from highest to lowest 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;
}
}
最好的方法是让你的 LetterCount class 成为 Comparable,这样你就可以使用库函数轻松地对其进行排序。将 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 如何将 class 的一个实例与另一个实例进行比较。这让 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());
}
}
我对如何去做有点迷茫,我知道我需要某种排序方法,正如您在我的注释代码中看到的那样,但我对哪些变量(参考和原始变量)感到困惑) 应该放在与构造方法有关的地方。我在自己的代码中迷失了一点。 下面是代码:
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;
}
}
最好的方法是让你的 LetterCount class 成为 Comparable,这样你就可以使用库函数轻松地对其进行排序。将 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 如何将 class 的一个实例与另一个实例进行比较。这让 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());
}
}