如题所述
通过HashSet剔除
// 删除ArrayList中重复元素,add进去顺序就变了不考虑顺序的话可以使用
public static void removeDuplicate1(List list) {
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
System.out.println(list);
}
// 删除ArrayList中重复元素,add进去顺序就变了不考虑顺序的话可以使用
public static void removeDuplicate1(List list) {
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
System.out.println(list);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2017-10-26
import java.util.Arrays;
public class Test{
public static void main(String[] args) {
int[] arr={1,2,2,3,4,4,5};
int [] temp=new int[0];
for(int i:arr){
if(!containArr(i, temp)){
temp=addArr(i, temp);
}
}
for(int i:temp)
System.out.println(i);
}
public static int[] addArr(int n,int [] temp){
int [] tempArr=Arrays.copyOf(temp, temp.length+1);
tempArr[temp.length]=n;
return tempArr;
}
public static boolean containArr(int n,int[] arr){
boolean flag=false;
for(int i:arr){
if(i==n){
flag=true;
break;
}
}
return flag;
}
}
public class Test{
public static void main(String[] args) {
int[] arr={1,2,2,3,4,4,5};
int [] temp=new int[0];
for(int i:arr){
if(!containArr(i, temp)){
temp=addArr(i, temp);
}
}
for(int i:temp)
System.out.println(i);
}
public static int[] addArr(int n,int [] temp){
int [] tempArr=Arrays.copyOf(temp, temp.length+1);
tempArr[temp.length]=n;
return tempArr;
}
public static boolean containArr(int n,int[] arr){
boolean flag=false;
for(int i:arr){
if(i==n){
flag=true;
break;
}
}
return flag;
}
}