方法一:利用Set集合去重(首推)
/**
* 使用Set集合去重
* @param numList
*/
private static void useSet(List<Integer> numList) {
Set<Integer> numSet = new HashSet<>();
for (int i = 0; i < numList.size(); i++) {
numSet.add(numList.get(i));
}
System.out.println();
System.out.println("=============="+numSet.size());
Iterator<Integer> iterator = numSet.iterator();
while(iterator.hasNext()){
System.out.print(" "+iterator.next());
}
}
方法二:使用双层for循环去重
//使用双层for循环去重
private static void useFor(List<Integer> numList) {
for (int i = 0; i < numList.size(); i++) {
for (int j = 0; j < numList.size(); j++) {
if(numList.get(i)==numList.get(j) &&i !=j){
numList.remove(j);
}
}
}
System.out.println();
System.out.println("==========="+numList.size());
for (Integer num:
numList) {
System.out.print(" "+num);
}
}
方法三:使用双层for循环
//使用双层for循环进行判断
private static void useFor2(List<Integer> numList) {
for (int i = 0; i < numList.size(); i++) {
for (int j = numList.size()-1; j > i; j--) {
if(numList.get(i)==numList.get(j)){
numList.remove(j);
}
}
}
}