它删除了 Array 中的重复项,但在接近末尾时跳过了一项。有人可以帮我解决这个问题吗?
输出将是这样的:
77 44 55 33 55 22 88 11 33 66 33
删除重复...
77 44 55 22 88 11 33
它跳过了'66'
应该打印的时间。
这是我的代码: HighArray.java
class HighArray {
private long[] a;
private int nElems;
public HighArray(int max) {
a = new long[max];
nElems = 0;
}
public boolean find(long searchKey) {
int j;
for(j=0; j<nElems; j++)
if(a[j] == searchKey)
break;
if(j == nElems)
return false;
else
return true;
}
public void insert(long value) {
a[nElems] = value;
nElems++;
}
public boolean delete(long value) {
int j;
for(j=0; j<nElems; j++)
if( value == a[j] )
break;
if(j==nElems)
return false;
else {
for(int k=j; k<nElems; k++)
a[k] = a[k+1];
nElems--;
return true;
}
}
public void noDups() {
System.out.println("\nRemoving duplicates...");
for(int i = 0; i<nElems; i++) {
for(int j = i+1; j<nElems; j++) {
if (a[i] == a[j]) {
delete(a[i]);
nElems--;
}
}
}
//return duplicates;
}
public void display(){
for(int j=0; j<nElems; j++)
System.out.print(a[j] + " ");
System.out.println("");
}
}
HighArrayApp.java
class HighArrayApp {
public static void main(String[] args) {
int maxSize = 100;
HighArray arr;
arr = new HighArray(maxSize);
arr.insert(77);
arr.insert(55);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(33);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(33);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display();
int searchKey = 35;
if( arr.find(searchKey) )
System.out.println("Found " + searchKey);
else
System.out.println("Can’t find " + searchKey);
arr.delete(00);
arr.delete(55);
arr.delete(99);
arr.display();
arr.noDups();
arr.display();
}
}