java中怎么样子找出数组中重复的数,并去除

如题所述

其实只要遍历数组,然后放进set集合内即可实现。
比如:
//set集合可以自动去重
Integer[] a = {1, 2, 2 , 19, 19, 8, 9};
Set<Integer> set = new HashSet<Integer>();
for(Integer i : a)
set.add(i);
for(Object s: set.toArray())
System.out.print(s+ " ");
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-03-07
int[] arr = new int[]{1,2,3,4,5,1,3,5};

Set<Integer> set = new HashSet<Integer>();
for(int i = 0 ; i < arr.length; i ++){
for( int j = 0 ; j < arr.length ;j ++){
if(i != j && arr[i] == arr[j]){
set.add(arr[i]);
}
}
}
System.out.println("重复的数字:"+set.toString());

Set<Integer> set2 = new HashSet<Integer>();
for(int i = 0 ; i < arr.length; i ++){
if(!set.contains(arr[i])){
set2.add(arr[i]);
}
}
System.out.println("删除重复数字后:"+set2);
第2个回答  2016-03-07
将数组中的值放在一个Set集合中,再将Set集合转为数组

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网