这是用 Java 编写的 while 循环。目的是从列表中删除所有重复项。但循环并没有中断。可能还有其他错误。
public static <E extends Comparable<? super E>> void removeDuplicates(ArrayList<E> L) {
ArrayList<E> temp = new ArrayList<E>(L.size());
ArrayList<Integer> index = new ArrayList<>(L.size());
int stop = 0;
while (true) {
//test for duplicates and save their indexes
for (int i = 0; i < L.size(); i++) {
if (!temp.contains(L.get(i))) {
temp.add(L.get(i));
index.add(i);
}
}
// if there were duplicates they will be removed
if (!index.isEmpty()) {
stop = 1;
for (int j = 0; j < index.size(); j++) {
L.remove(index.get(j));
}
}
//if nothing is removed there should be no duplicates and the loop should break
if (stop == 0) {
break;
}
index.clear();
temp.clear();
stop = 0;
}
}
慕的地6264312
相关分类