统计数组中的字符串出现的次数

取字符串中的字符 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);
    }
}

发布了65 篇原创文章 · 获赞 13 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/weixin_42498050/article/details/105212315
今日推荐