删除在java中的ArrayList重复 [英] delete duplicates in java arraylist

查看:192
本文介绍了删除在java中的ArrayList重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢马尔科。我重写了code。尽量做到简单。这一次,它真的可以编译。但它只能在下删除重复项坐在对方。例如,如果我把1 2 3 3 4 4 5 1 - 输出为1 2 3 4 5 1。它不能在最后拿起重复。
(BTW:新来这个网站,如果做任何显示的混乱表示歉意)

下面是新的code:

 进口的java.util。*;公共类SetListDemo {
公共静态无效的主要(字串[] args){
    SetListType newList =新SetListType();
    扫描仪键盘=新的扫描仪(System.in);    的System.out.println(输入一系列的项目:);
        字符串输入= keyboard.nextLine();    的String [] =原input.split();
    对于(一个String:原创)
    newList.insert(多个);    清单<串GT; finalList =新的ArrayList(Arrays.asList(原件));    迭代器<串GT; setIterator = finalList.iterator();    字符串的位置= NULL;    而(setIterator.hasNext()){
        串secondItem = setIterator.next();        如果(secondItem.equals(位置)){
            setIterator.remove();
        }        位置= secondItem;
    }    的System.out.println(\\ nHere是集列表:);
    DisplayList中(finalList);
    的System.out.println(\\ n);
}公共静态无效DisplayList中(名单列表){
    对于(INT指数= 0;指数 - LT;则为list.size();指数++)
    System.out.print(list.get(指数)+,);
}}


解决方案

要回答这个问题删除的Java数组列表重复:

只要把所有元素成设置键,你就大功告成了。

- 或 -

迭代你的原创清单和元素添加到列表,但加入他们之前,请与列表包含#()如果元素已经存在。

编辑:试试这个:

 的String [] =原input.split();
清单<串GT; finalList =新的ArrayList<串GT;();对于(一个String:原创){
    如果(!finalList.contains(多个)){
        finalList.add(多个);
    }
}的System.out.println(\\ nHere是集列表:);
DisplayList中(finalList);
的System.out.println(\\ n);

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

}

解决方案

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

这篇关于删除在java中的ArrayList重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆