- public static void main(String[] args)
- {
- // 1、取出一个字符串中字母出现的次数
- String str="akllm";
- char[] chs=str.toCharArray();
- StringBuilder sb=new StringBuilder();
- TreeMap<Character, Integer> map=new TreeMap<Character, Integer>();
- for (int x = 0; x < chs.length; x++)
- {
- Integer value=map.get(chs[x]);
- if (value==null)
- {
- map.put(chs[x], 1);
- }
- else {
-
- map.put(chs[x], (value+1));
- }
- }
- Set<Character> set=map.keySet();
- Iterator<Character> it=set.iterator();
- while(it.hasNext())
- {
- Character key=it.next();
- Integer value=map.get(key);
- sb.append(key+"["+value+"]");
- }
- System.out.println(sb.toString());
-
-
- }
复制代码
当把sb.append(key+"["+value+"]");这句中的key换成it.next()时,运行结果就会出错,我的理解这两种写法应该等效啊,为什么会出错呢 |
|