简体   繁体   English

删除Java arraylist中的重复项

[英]delete duplicates in java arraylist

Thanks Marko. 谢谢Marko。 I rewrite the code. 我重写代码。 try to make it simple. 尝试使其简单。 this time it can really compile. 这次它可以真正编译。 but it can only delete duplicate items sit next to each other. 但它只能删除彼此相邻的重复项。 for example, if i put in 1 2 3 3 4 4 5 1 -- the output is 1 2 3 4 5 1. it can't pick up the duplicate at the end. 例如,如果我输入1 2 3 3 4 4 5 1,则输出为1 2 3 4 5 1。它最后无法拾取重复项。 (BTW: new to this website, if make any display mess my apologies) (顺便说一句:本网站的新内容,如果让任何显示弄乱我的歉意)

here's the new code: 这是新代码:

import java.util.*;

public class SetListDemo{
public static void main(String[] args){
    SetListType newList = new SetListType();
    Scanner keyboard = new Scanner(System.in);

    System.out.println( "Enter a series of items: ");
        String input = keyboard.nextLine();

    String[] original = input.split(" ");
    for (String s : original)
    newList.insert(s);

    List<String> finalList = new ArrayList(Arrays.asList(original)) ;

    Iterator<String> setIterator = finalList.iterator();  

    String position = null;

    while(setIterator.hasNext()){
        String secondItem = setIterator.next();

        if(secondItem.equals(position)){
            setIterator.remove();
        }   

        position = secondItem;
    }

    System.out.println("\nHere is the set list:");
    displayList(finalList);
    System.out.println("\n");
}

public static void displayList(List list){
    for(int index = 0; index <list.size(); index++)
    System.out.print(list.get(index) + ", ");
}

}

To answer the question "delete duplicates in java arraylist": 要回答问题“删除java arraylist中的重复项”:

Just put all elements into a Set and you're done. 只需将所有元素放入Set就可以了。

-or- -要么-

Iterate your original list and add the elements to a List , but before adding them, check with List#contains() if the element is already there. 迭代original列表并将元素添加到List ,但是在添加元素之前,请使用List#contains()检查元素是否已存在。

EDIT: Try this: 编辑:试试这个:

String[] original = input.split(" ");
List<String> finalList = new ArrayList<String>();

for (String s : original) {
    if (!finalList.contains(s)) {
        finalList.add(s);
    }
}

System.out.println("\nHere is the set list:");
displayList(finalList);
System.out.println("\n");

SetListIterator is a class indirectly referenced by your code, but it is not on the classpath. SetListIterator是您的代码间接引用的类,但不在类路径上。 When setting up your project you forgot to copy that source file in addition to SetListType , or it could be that you are compiling this outside an IDE and simply failed to compile that class. 设置项目时,除了SetListType ,您还忘记了复制该源文件,或者可能是您在IDE外部对其进行了编译,而只是无法编译该类。

From what you're saying it sounds like when you run your assignment you are not setting your classpath correctly so that it includes the compiled class file of the SetListType. 用你的话说,这听起来像是在运行任务时,您没有正确设置类路径,因此它包含SetListType的已编译类文件。 You should be able to fix this by setting the -classpath option when running your main method to point to this and any other classes your assignment relies on. 您可以通过在运行main方法指向该代码以及分配所依赖的任何其他类时设置-classpath选项来解决此问题。

You can use Vector or ListArray and check if the element exists in the new list before you add it. 您可以使用Vector或ListArray并在添加元素之前检查该元素是否存在于新列表中。

Just an example: 只是一个例子:

    Vector<String> list = new Vector<String>();
    System.out.println("list:");
    for(int i=0; i<100; i++){
        list.add("" + new Random().nextInt(10));
        System.out.println(list.lastElement());
    }

    System.out.println("newList:");
    java.util.Iterator<String> it = list.iterator();
    Vector<String> newList = new Vector<String>();
    while(it.hasNext()){
        String s = it.next();
        if(!newList.contains(s)){
            newList.add(s);
        }
    }

    for(String s : newList){
        System.out.println(s);
    }

For the second part: 对于第二部分:

    int[] count = new int[newList.size()];      
    for(String s : list){
        int index = newList.indexOf(s);
        count[index]++;
    }

    for(String s : newList){
        System.out.println(s + " appears " + count[newList.indexOf(s)] + " times");
    }

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

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