删除重复使用方法

时间:2014-11-06 20:56:35

标签: java arrays list duplicates erase-remove-idiom

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

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) + " ");
     }
}

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

2 个答案:

答案 0 :(得分:2)

如果我理解正确,你应该用这个标题实现一个方法

public static void removeDuplicate(ArrayList<Integer> list)

根据其名称判断,我说该方法应该从列表中删除重复项,而不是(正如您现在所做的那样)在输入过程中执行do-while循环。

首先首先删除循环中的检查(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
       }

然而,这只会删除第一个重复。或者,您可以删除列表中与current整数相等的每个项目:

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

就是这样。你的方法已经完成。

    }
}

它已完成,因为您实际上正在处理程序中唯一的列表。即使您从sublist中删除了一个整数,它实际上也会从sublist真实列表中删除(sublist只是一个参考,而不是一个实际的清单)

修改

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

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

答案 1 :(得分:0)

如果您不想复制,请使用实现Set接口(http://docs.oracle.com/javase/7/docs/api/java/util/Set.html)而不是数组列表的集合。