欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

面试试题002--数字母的个数

程序员文章站 2022-05-24 07:54:38
...
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myjava;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class LetterCount {

    public static void main(String[] args) {

        String str = "ajsfkjjkejrfjajjjjj";
        HashMap hm = new HashMap();
        //char[] a = str.toCharArray();
        // System.out.println(a.length);
        for (int i = 0; i < str.length(); i++) {
            if (!hm.containsKey(str.charAt(i))) {
                hm.put(str.charAt(i), 1);
            } else {
                Integer count = (Integer) hm.get(str.charAt(i));
                count++;
                hm.put(str.charAt(i), count);
            }
        }
 Set key = hm.keySet();
 System.out.println("一共有"+key.size()+"个字母!");
for(Iterator itr = key.iterator();itr.hasNext();){
   Character letter =((Character)itr.next());
    System.out.print(letter+":      ");
   System.out.println(hm.get(letter));


}
    }
}


 

 

相关标签: 面试 Java