package HashMapUse;
import java.util.HashMap;
public class HashMapUseDemo01 {
public static void main(String[] args) {
int arr[]={1,1,5,5,3,3,3};
HashMap<Integer, Integer> integerIntegerHashMap = new HashMap<>();
count(arr,integerIntegerHashMap);
System.out.println(integerIntegerHashMap);
}
public static void count(int arr[], HashMap<Integer,Integer> hashMap){
for (int a:arr
) {
Integer integer = hashMap.get(a);
if (integer==null){
hashMap.put(a,1);
}
else {
hashMap.put(a,integer+1);
}
}
}
}
使用HashMap统计数组中每个数出现的次数
最新推荐文章于 2024-04-26 18:33:51 发布