gpt4 book ai didi

java - 为什么我的 removeDuplicates 方法只在第一次遇到重复整数时删除它?

转载 作者:行者123 更新时间:2023-11-30 08:26:48 25 4
gpt4 key购买 nike

<分区>

我删除重复数字的方法有效,但如果数字出现两次以上则无效。例如使用该方法时,带有数字 1,2,2,3,4,5,6,7,7,7,8,9 的列表给出列表 1,2,3,4,5,6,7,7, 8,9

import java.util.*;
public class final SortAndRemove{
private SortAndRemove(){
}
public static void selectionSort(List<Integer> a){
if(a == null)
return;
if (a.size() == 0 || a.size() == 1)
return;

int smallest;
int smallestIndex;
for (int curIndex = 0; curIndex < a.size(); curIndex++) {

smallest = a.get(curIndex);
smallestIndex = curIndex;

for (int i = curIndex + 1; i < a.size(); i++) {
if (smallest > a.get(i)) {

smallest = a.get(i);
smallestIndex = i;
}
}


if (smallestIndex == curIndex);

else {
int temp = a.get(curIndex);
a.set(curIndex, a.get(smallestIndex));
a.set(smallestIndex, temp);
}

}
}


public static void removeDuplicates(List<Integer> a){
if(a == null)
return;
if (a.size() == 0 || a.size() == 1)
return;
for(int curIndex = 0; curIndex <a.size(); curIndex++){
int num = a.get(curIndex);
for(int i = curIndex + 1; i < a.size(); i++){
if(num == a.get(i))
a.remove(i);
}

}

}

}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com