桃花长相依
一种直接的方法是使用2个数组,一个用于存储字符串中出现的字符,区分大小写,另一个用于存储其相应的频率。两个数组都使用字符串长度的大小进行声明,以下示例代码展示了如何仅使用数组和简单的逻辑来实现您想要的效果。示例代码String str = "The array must be in alphabetical order and also store non-alphabetical characters (including spaces)";char[] charArr = new char[str.length()];int[] freqArr = new int[str.length()];int idx = 0;for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); boolean isFound = false; for (idx = 0; idx < str.length(); idx++) { if (freqArr[idx] == 0) { break; } if (c == charArr[idx]) { freqArr[idx]++; isFound = true; break; } } if (!isFound) { charArr[idx] = c; freqArr[idx]++; }}//Sort charArr and freqArr arrayschar tempChar;int tempFreq;for (int i = 1; i < str.length(); i++) { for (int j = i; j > 0; j--) { if (charArr[j] < charArr [j - 1]) { tempChar = charArr[j]; charArr[j] = charArr[j - 1]; charArr[j - 1] = tempChar; tempFreq = freqArr[j]; freqArr[j] = freqArr[j - 1]; freqArr[j - 1] = tempFreq; } }}打印字母频率for (int i = 0; i < str.length(); i++) { if (freqArr[i] != 0) { System.out.printf("%s:%d", charArr[i], freqArr[i]); System.out.println(); }}控制台输出[:13],[(:1],[):1],[-:1],[T:1],[a:13],[b:3],[c:6],[d: 3],[e:8],...