您的位置:首页 > 其它

给一个字符串,包含了空格等标点符号,计算出出现次数最多的字母和该字母出现的次数

2009-08-13 22:37 676 查看
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

public class TestStringSplict {

public static void main(String[] args){
String str = "aaaaaaaccccccccccccccccccccccaaaabb";
// 用map实现
// TreeMap<Character,Integer> map = new TreeMap<Character,Integer>();
// for(Character ch : str.toCharArray()){
// if((ch>='a' && ch<'z')||(ch>'A' && ch<'Z')){
// Integer count = map.get(ch);
// map.put(ch, null==count?1:count+1);
// }
// }
//
// System.out.println(Collections.max(map.values()));


//用普通数组实现
int[] aa = new int[60];
for(char temp:str.toCharArray()){
if((temp>=65 && temp<=90)||(temp>=97 && temp<=122)){
temp -= 65;
aa[temp]++;
}
}
int max = aa[0]; int position = 0;
for(int i=0;i<aa.length;i++){
if(aa[i]>max){
max = aa[i]; position = i;
}
}
System.out.println(max);
System.out.println("字母"+(char)(position+65) + "出现" + max + "次");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐