有一个数组,String [] a={ads,awww,fdgfdf,fvdsfc,hyjutfrdf},
题目要求,例如,数组a={ads}中,在a-z共二十六个字母中,字母a出现了1次,d出现了1次,s出现了1次,其他字母出现了0次,求出在以上数组中,打印出各个字母都出现了多少次。
呵呵,求高人给大家解答分享下。

解决方案 »

  1.   

     用hashmap保存 key值是字符 value是次数
      

  2.   

    利用ascii码值的范围很小[0, 255],直接定义一个大小为256的数组,这样解决办法可以简化很多。
    public class Hello {
        public static void main(String[] args) {
            String [] a = {"ads", "awww", "fdgfdf", "fvdsfc", "hyjutfrdf"};
            int[] frequence = new int[256];        for (String str : a) {
                for (int i = 0; i < str.length(); ++i) {
                    frequence[str.charAt(i)]++;
                }
            }        for (int i = 0; i < frequence.length; ++i) {
                if (frequence[i] != 0) {
                    System.out.printf("%s: %d\n", (char)i, frequence[i]);
                }
            }
        }
    }
    a: 2
    c: 1
    d: 5
    f: 7
    g: 1
    h: 1
    j: 1
    r: 1
    s: 2
    t: 1
    u: 1
    v: 1
    w: 3
    y: 1
      

  3.   

    import java.util.Iterator;
    import java.util.Map;
    import java.util.TreeMap;public class StringCount
    {
    public static void main(String[] args)
    {

    String[] arr ={"ads","awww","fdgfdf","fdgfdf","fdgfdf"};

    System.out.println(getCount(arr));;
    }

    private static String getCount(String[] strs)
    {
    StringBuilder builder = new StringBuilder();
    for(int x=0; x<strs.length; x++)
    {
    builder.append(strs[x]);
    }

    String s = builder.toString();

    char[] arr = s.toCharArray();

    Map<Character,Integer> map = new TreeMap<Character,Integer>();
    int count = 1;

    for(int x=0; x<arr.length; x++)
    {
    if(!(arr[x]>='a' && arr[x]<='z' || arr[x]>='A' && arr[x]<='Z'))
    continue;

    if(!(map.containsKey(arr[x])))
    map.put(arr[x], count);
    else
    count = map.get(arr[x]) + 1;
    map.put(arr[x], count);
    count = 1;
    }

    StringBuilder sb = new StringBuilder();

    Iterator<Map.Entry<Character, Integer>> it = map.entrySet().iterator();

    while(it.hasNext())
    {
    Map.Entry<Character, Integer> me = it.next();
    char key = me.getKey();
    int value =  me.getValue();

    sb.append(key+":("+value+")");
    }
    return sb.toString();
    }
    }
      

  4.   

    2L的思维很巧妙啊~优化下:根据题目要求,a-z 26个,初始化数组26即可,我们可以循环的时候判断下,小于'a'或大于'z'就跳过好了~
      

  5.   

    public class times { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String[] s = {"ads","awww","fdgfdf","fvdsfc","hyjutfrdf"};
    int[] len = new int[26];
    for(int i = 0;i < s.length;i++){
    for(int j = 0;j <s[i].length();j++){
    len[s[i].charAt(j) - 'a']++;
    }
    }
    for(int i = 0;i < 26;i++){
    char temp;
    temp = (char)('a' + i);
    System.out.print(temp);
    System.out.println(":"+len[i]);
    } }}
      

  6.   


    仿照Thanking in Java的例子写的public class Test {
    public static void main(String[] args) {

    String[] a={"ads","awww","fdgfdf","fvdsfc","hyjutfrdf"};
    Map<String,Integer>map=new HashMap<String,Integer>();
    for(int n=0;n<a.length;n++){
    for(int i=0;i<a[n].length();i++){
    Integer fre=map.get(String.valueOf(a[n].charAt(i)));
    map.put(String.valueOf(a[n].charAt(i)), fre==null ? 1:fre+1);
    }
    }
    System.out.println(map);
    }

    }运行结果{f=7, g=1, d=5, c=1, a=2, j=1, h=1, w=3, v=1, u=1, t=1, s=2, r=1, y=1}
      

  7.   

    public class Test {
    public static void main(String[] args) { String[] a = { "ads", "awww", "fdgfdf", "fvdsfc", "hyjutfrdf" };
    Map<Character, Integer> map = new HashMap<Character, Integer>();
    for (int n = 0; n < a.length; n++) {
    for (int i = 0; i < a[n].length(); i++) {
    Integer fre = map.get(a[n].charAt(i));//不存在返回null否则返回当前出现的次数
    map.put(a[n].charAt(i), fre == null ? 1 : fre + 1);
    }
    }
    System.out.println(map);
    }
    }