統計數組中的字符串出現的次數

取字符串中的字符 charAt(j);

取數組中的元素 array[i];

 

package day10.abs;

import java.util.HashMap;

public class CountAll {


    public static void main(String[] args) {
        HashMap hm = new HashMap();

        String B = "AABDDSEE";
        for (int j = 0; j < B.length(); j++) {
            char b = B.charAt(j);
            System.out.println("字符串b爲:" + b);
        }

        String[] A = {"AAA", "A", "A", "B", "C", "C", "D", "S", "D", "D", "D"};

        for (int i = 0; i <= A.length - 1; i++) {

            String key = A[i];
            System.out.println("數組key爲:" + key);
            Object ob = hm.get(key);
            if (ob == null) {
                // 如果key對應的value沒有,第0次
                hm.put(key, 1);
            } else {
                int value = Integer.parseInt(ob.toString());
                hm.put(key, value + 1);
            }
        }

        System.out.println(hm);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章