本帖最后由 王博 于 2012-12-14 17:51 编辑
/* "asdfgzxcavasdfxavdf"获取该字符中字母出现的次数 希望打印结果a(1)b(2)... */ import java.util.*; class MapTest3 { public static void main(String[] args) { String s =charCount("asd==fgzxcavasdfxavdf"); System.out.println(s); System.out.println("OVER!"); } public static String charCount(String str) { char[] ch = str.toCharArray(); TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>(); int count=0; for (int i=0;i<ch.length ;i++ ) { if(!(chs[x]>='a' && chs[x]<='z' || chs[x]>='A' && chs[x]<='Z')) //这里是什么意思啊?? continue; //continue是什么啊?毕老师说的太少了,不明白啊,求解答 Integer value = tm.get(ch); if (value!=null) { count=value; } count++; tm.put(ch,count); count=0; } StringBuffer sb = new StringBuffer(); Set<Map.Entry<Character,Integer>> entrySet = tm.entrySet(); Iterator<Map.Entry<Character,Integer>> it = entrySet.iterator(); while (it.hasNext()) { Map.Entry<Character,Integer> me = it.next(); Character chs=me.getKey(); Integer in=me.getValue(); sb.append(chs+"("+in+") "); } //System.out.println(tm); return sb.toString(); } } |