去掉数组中相同的值

01 |
//去掉数组中相同的项 |
02 |
//我们知道,数组的项是可以重复的,有时候我们需要去掉数组中重复的项,怎么办? |
03 |
//我们也知道,HashSet里面是没有重复的项的,我们可不可以将数组转化为HashSet,然后再转化? |
04 |
|
05 |
String[] strs1 = new String[]{"8","1","1","3","3","2","11","7"}; |
06 |
HashSet set = new HashSet(); |
07 |
set.addAll(Arrays.asList(strs1)); |
08 |
//Arrays.asList将数组转List,addAl将Collection转化为HashSet |
09 |
String[] strs2 = (String[])set.toArray(new String[0]); |
10 |
for(int i=0;i<strs2.length;i++){ |
11 |
System.out.println(strs2[i]); |
12 |
} |


浙公网安备 33010602011771号