0

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

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

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

4

2 回答 2

2

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

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
       }

然而,这只会删除第一个副本。或者,您可以删除列表中等于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);
        }
    }
}
于 2014-11-06T21:43:38.407 回答
0

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

于 2014-11-06T21:20:28.877 回答