public class Mytest {    public static void main(String[] args) {
        String aa = "this is a pig";
        Map temp = new HashMap();
        for (int i = 0; i < aa.length(); i++) {
            String a = aa.substring(i, i+1);
            if (temp.get(a) == null) {
                temp.put(a, new Integer(1));
            } else {
                int count = ((Integer) temp.get(a)).intValue();
                temp.put(a, new Integer(count + 1));
            }
        }
        
        Set dataSet = temp.entrySet();
        Iterator iter = dataSet.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }
    }
}

解决方案 »

  1.   

    最后两行写成这样吧简单一点
    while (iter.hasNext()) {
        Map.Entry entry = (Map.Entry)iter.next();
        System.out.println(entry.getKey() + "'s count is " + entry.getValue());
    }
      

  2.   

    如果仅处理英文字符,且不考虑字符出现的顺序:String s = "this is a story!";
    int[] counts = new int[128];
    byte[] bytes = s.getBytes();
    for(int i=0; i<bytes.length; i++)
    counts[0x7f & bytes[i]]++; 
    for(int i=0; i<counts.length; i++)
    {
    if(counts[i] != 0)
    System.out.println(((char) i) + "=" + counts[i]); 
    }
      

  3.   

    please give me the TIAO SHI GUO DE programe
     zhong xie
      

  4.   

    补充一下,上面whyxx的Mytest类前少导入包了(初学者有时不太容易调错,其实我也只是初学者),应该在前面添加:
    import java.util.*;
    如有冒犯,还请见谅.
      

  5.   

    how to do the program with hashmap?
      

  6.   

    Why hashmap?To kill a chicken with cow knife!