0
我被给了一个面试问题来输出最常出现的角色。Java隐式转换字符为int?
鉴于此字符串“aaxxaabbaa”。字符'a'是最常见的。
下面的代码是我发现在互联网上搜索的东西。注意:我实现其与2个环,其是在高效(不同的主题)
public static char findMostUsedChar(String str){
char maxchar = ' ';
int maxcnt = 0;
// if you are confident that your input will be only ascii, then this array can be size 128.
// Create a character counter
**int[] charcnt = new int[Character.MAX_VALUE + 1];**
for(int i = 0; i < str.length()-1; i++){
char **ch** = str.charAt(i);
// increment this character's cnt and compare it to our max.
if (**charcnt[ch]**++ >= maxcnt) {
maxcnt = charcnt[ch];
maxchar = ch;
}
}
return maxchar;
}
他们宣称int数组,发现字符在特定的指数(即“A”),然后用作指标。 在eclipse中的调试器上追踪代码后,我仍然不明白如何使用字符来表示int索引,而不显式地转换它或使用charVal.getNumericValue()?即使大部分S.O.字符显式转换为int主题。
在此先感谢。
http://stackoverflow.com/questions/22501867/how-to-set-array-dimension-to-long-number –