在java中,给定一个数组(2,4,1,6,0,8,2,6,0,3,8,6,7,4,2,1),请

计出每个数
2025-03-07 05:29:17
推荐回答(5个)
回答1:

public static void count()

    {

        int[] a = {2,4,1,6,0,8,2,6,0,3,8,6,7,4,2,1};

        Map countMap = new HashMap();

        for (int i = 0; i < a.length; i++)

        {

            if (countMap.get(a[i]) == null)

            {

                countMap.put(a[i], 1);

            }

            else

            {

                countMap.put(a[i], countMap.get(a[i]) + 1);

            }

        }

        System.out.println(countMap.toString());

    }

如果是每个数的个数


回答2:

你是想说统计各个数值出现次数吧

public static void main(String[] args) {
HashMap map = new HashMap();
int[] numbs = {2, 4, 1, 6, 0, 8, 2, 6, 0, 4, 8, 6, 4, 7, 2, 1};
for (int i = 0; i < numbs.length; i++) {

if (map.containsKey(numbs[i])) {
map.put(numbs[i], (int) map.get(numbs[i]) + 1);
} else {
map.put(numbs[i], 1);
}

}

Set set = map.keySet();
Object[] objects = set.toArray();
for (int i = 0; i < objects.length; i++) {
System.out.println(objects[i] + "出现" + map.get(objects[i])+"次");

}


}

回答3:

使用Map去计算。。。。。。。

回答4:

计出每个数 什么意思?

回答5:

每个数的个数吗?