第1个回答 2022-06-11
public static void main(String[] args) {
Set<Integer> set=new HashSet<Integer>(); //计数
int count=0;
int[] ary={10,12,12,2,2,34,13};
/*
* 遍历数组,将不重复的数加入进去,应为Set集合
* 是不能add相同元素的,如果加入的数,里面有,则set.add(a)的返回值为false
*/
for(Integer a:ary){
set.add(a);
}
//集合的大小就是不重复元素的个数
System.out.println("有"+set.size()+"个不同的数,其中:");
/*外层for循环获取set集合中每一个元素,与数组
*中的每个元素比较,如果相同count++,
*比较完之后,得到个数。然后将count归0,进行下个数的比较
*/
for( Integer key:set){
for(int i=0;i<ary.length;i++){
if(key==ary[i]){
count++;
}
}
System.out.print(key+"的个数为:"+count+" ");
count=0;
}
}