繁体   English   中英

noDups() 方法从数组中删除所有重复项

[英]noDups()method to remove all duplicates from the array

它删除 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("");
}

}

高阵列应用程序

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();
}

}

在遍历数组时不应修改索引,否则会看到一些奇怪的结果。 迭代肯定会跳过一些元素,因为它们的索引不是应该的。

假设您正在像这样迭代数组:

0 1 2 3 4 5  // indices
1 2 5 6 7 8  // array elements
^
i            // current position of i

现在删除索引0处的元素。 然后所有剩余的元素将向左移动,但i会继续前进。 删除索引0处的元素后,数组结构如下:

0 1 2 3 4   // indices
2 5 6 7 8   // array elements
  ^
  i         // current position of i (Will move from 0 to 1)

看,下一个处理的元素将是5 ,而不是2 这就是您的代码跳过元素的原因。


您可以通过向后迭代数组来解决此问题,这不会修改要处理的剩余元素的索引。 哦,你不需要嵌套 for 循环。 只需单个 for 循环即可完成任务。

像这样修改你的循环:

for(int i = nElems; i > 0; i--) {
        if (a[i] == a[i + 1]) {
            delete(a[i]);
         // nElems--; // Not needed here. (You're already doing this in delete())
        }
    }

说了这么多,考虑使用Set来完成这个任务。 这就是 Java API 中有Set原因。 它会自动删除重复的元素。

你应该试试这个。 在 JDK 中,我们有许多这样的实用程序类。

public static void main(String[] args){

    Long [] a = {77l, 44l, 55l, 33l, 55l, 22l, 88l, 11l, 33l, 66l, 33l};

    Set<Long> set=new HashSet<>(Arrays.asList(a));
    System.out.println(set);
}

您遇到的一个问题是您调用nElems--; 两次 - 一次在delete (这很公平)和一次在noDups ,在调用delete之后立即。 你应该删除后者。

第二个问题是 Rohit 发现的问题。

我是编程新手,但请参阅下面的尝试。 我认为它有效。

public void noDups(){       
  int i;
  int j;

  for(i = nElems-1;i>=0;i--){
     for(j = i-1;j>=0;j--){
        if(a[i]==a[j]){
            delete(a[j]);
        }
     }
  }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM