- import java.util.*;
- class MapTest
- {
- public static void main(String[] args)
- {
- String s = myTest("zzfdqavcbsacdfs");
- System.out.println(s);
- }
- public static String myTest(String s)
- {
- char[] ch = s.toCharArray();
- TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
- for (int i = 0;i<ch.length ; i++ )
- {
- if(!(ch[i]>='a' && ch[i]<='z' || ch[i]>='A' && ch[i]<='Z'))
- continue;
- int value = 1;
- if(!tm.containsKey(ch[i]))
- tm.put(ch[i],value);
- else
- {
- value = tm.get(ch[i]);
- value += 1;
- tm.put(ch[i],value);
- }
- }
- //System.out.println(tm);
- Iterator it = tm.keySet().iterator();
- StringBuilder sb = new StringBuilder();
- while(it.hasNext())
- {
- Character key = it.next(); //此会编译时报错 ,说Object无法转化为Charactor 。要写成Object key 才行。为什么?
- Integer v = tm.get(key);
-
- sb.append(key+"("+v+")");
- }
- return sb.toString();
- }
- }
复制代码 在我注释的地方会报错,求解答?
StringMathTest.java:35: 错误: 不兼容的类型: Object无法转换为Character
Character key = it.next();
^
|
|