java - 使用方法删除重复项

标签 java arrays list duplicates erase-remove-idiom

我构建了删除重复项,但是我正在考虑如何使用一种方法,使用以下 header 从整数数组列表中删除重复元素:

public static void removeDuplicate(ArrayList<Integer> list)

编写一个测试程序,提示用户在列表中输入 10 个整数并 显示由一个空格分隔的不同整数。

import java.util.ArrayList;

import java.util.Scanner;

public class RemoveDuplicates {
    public static void main(String[] args){
     ArrayList<Integer>list = new ArrayList<Integer>();

     Scanner input = new Scanner (System.in);
     System.out.print("Enter integers (input ends with 0): ");
     int value;

     do{
         value = input.nextInt();
         if(!list.contains(value)&& value !=0)
             list.add(value);
     }while (value !=0);

     input.close();
     for (int i = 0; i < list. size(); i++)
          System.out.print(list.get(i) + " ");
     }
}

这是我的代码,请修改一下,如何使用方法和测试。

最佳答案

如果我理解正确,您应该使用此 header 实现一个方法

public static void removeDuplicate(ArrayList<Integer> list)

从它的名称来看,我想说该方法应该从列表中删除重复项,而不是(正如您现在所做的那样)输入期间的 do-while-loop。

因此,首先删除循环中的检查 (if(!list.contains(value)&& value !=0)),然后添加用户输入的每个数字到列表中。

然后您可以调用方法removeDuplicate(list);。如果您愿意,可以在循环中添加此调用,它将在每次输入后执行,或者在输入关闭时仅执行一次。

现在实现该方法:

public static void removeDuplicate(ArrayList<Integer> list) {  // this is the header you need to use

这里的问题是,该方法知道列表,但不知道可能重复的元素。所以你必须寻找它

    for (int i = 0; i < list.size(); i++) {  // iterate through every element in the list 
        Integer current = list.get(i);       // for convenience, save the current list item in a variable

因此,您逐一检查列表中的每个整数..但是如果您想知道该整数是否第二次存在,则必须搜索列表的尾部。这意味着你必须检查 i 之后的子列表。

        List sublist = list.subList(i + 1, list.size());  // the sublist with all elements of the list from i+1 to the end

您的 list.contains(value) 行是正确的,您也可以在此处使用它。只是现在你在子列表上调用它

       if(sublist.contains(current)){   // checks if the number is in the sublist
             sublist.remove(current);   // removes the number from the sublist
       }

但这只会删除第一个重复项。或者,您可以删除列表中等于当前整数的每个项目:

        while (sublist.contains(current)) {
            sublist.remove(current);
        }

就是这样。您的方法已完成。

    }
}

它已经完成,因为您实际上正在处理程序中唯一的列表。即使您从子列表中删除一个整数,它实际上也会从子列表真实列表(子列表 只是一个引用,本身并不是一个实际列表)

编辑

为了您的方便,这里提供了两种方法的完整代码。如果您将代码与您的代码进行比较,您会发现没有太大不同:

public static void main(String[] args) {
    ArrayList<Integer> list = new ArrayList<Integer>();

    Scanner input = new Scanner(System.in);
    System.out.print("Enter integers (input ends with 0): ");
    int value;

    do {
        value = input.nextInt();
        if (value != 0) {     // this changed: add every number except 0
            list.add(value);
        }
    } while (value != 0);

    input.close();

    removeDuplicate(list);    // here you make the call for the new method

    for (int i = 0; i < list.size(); i++) {
        System.out.print(list.get(i) + " ");
    }
}

// and this is the new method
public static void removeDuplicate(ArrayList<Integer> list) {
    for (int i = 0; i < list.size(); i++) {
        Integer current = list.get(i);
        List sublist = list.subList(i + 1, list.size());
        while (sublist.contains(current)) {
            sublist.remove(current);
        }
    }
}

关于java - 使用方法删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26789237/

相关文章:

arrays - 如何在qt中生成JsonRpc请求?

actionscript-3 - AS3 - 压缩 ByteArray

.net - 如何通过对象值从 List(Of myClass) 中删除对象?

java - 为什么序列化 Integer 需要这么多(81)个字节?

java - jsp变量作为javascript函数参数

java - Java 的 x509 证书解析库

从 CSV 数据构建 C 数组

java - 使用 jsoup 读取 XML

javascript 循环遍历 li 属性并更改每个项目的值

c - 从文件分配列表时出现段错误