我有这个工作正常的代码,但问题是结果如下所示。
public static int[] countlist (char[] list){
int [] counts = new int[list.length];
for (int k = 0; k < list.length; k++) {
for (int m = 0; m < list.length; m++) {
if (list[m] == list[k]){
counts[m]++;
}
}
System.out.println( "Letter " + list[k] + " = " + counts[k]);
}
}
输出:
Letter T = 1
Letter T = 2
Letter N = 1
Letter T = 3
Letter Z = 1
Letter N = 2
Letter H = 1
Letter H = 2
我该怎么做才能获得每个字母的输出一次?非常感谢例如,我希望输出如下所示
Letter T = 3
Letter N = 2
Letter Z = 1
Letter H = 2