delete duplicates in java arraylist

こ雲淡風輕ζ 提交于 2019-12-20 04:19:47

问题


Thanks 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. (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) + ", ");
}

}

回答1:


To answer the question "delete duplicates in java arraylist":

Just put all elements into a Set and you're done.

-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.

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



回答2:


SetListIterator is a class indirectly referenced by your code, but it is not on the classpath. 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.




回答3:


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. 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.




回答4:


You can use Vector or ListArray and check if the element exists in the new list before you add it.

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


来源:https://stackoverflow.com/questions/12196762/delete-duplicates-in-java-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!