6

我的逻辑混乱了。我只是想找出一种在不使用 HashSet 的情况下删除 ArrayList 中的重复项的方法。

public static void main(String[] args) {
    ArrayList<String> wordDulicate = new ArrayList<String>();
    wordDulicate.add("Tom");
    wordDulicate.add("Jones");
    wordDulicate.add("Sam");
    wordDulicate.add("Jamie");
    wordDulicate.add("Robie");
    wordDulicate.add("Helen");
    wordDulicate.add("Tom");
    wordDulicate.add("Troy");
    wordDulicate.add("Mika");
    wordDulicate.add("Tom");

    for (String dupWord : wordDulicate) {
        if (wordDulicate.equals(dupWord))
            System.out.println(wordDulicate.get(dupWord));
    }
}
4

8 回答 8

9

您可以创建另一个列表,并向其中添加项目,跳过重复项:-

ArrayList<String> wordDulicate = new ArrayList<String>();
ArrayList<String> tempList= new ArrayList<String>();

wordDulicate.add("Tom");
wordDulicate.add("Jones");
wordDulicate.add("Sam");


for (String dupWord : wordDulicate) {
    if (!tempList.contains(dupWord)) {
        tempList.add(dupWord);
    }
}
于 2012-10-02T19:49:38.227 回答
6

您应该对列表进行排序并删除与前一个相同的元素。

于 2012-10-02T19:49:02.397 回答
6

这种方法需要有另一个列表:

       ArrayList<String> wordDulicate = new ArrayList<String>();

        wordDulicate.add("Tom");
        wordDulicate.add("Jones");
        wordDulicate.add("Sam");
        wordDulicate.add("Jamie");
        wordDulicate.add("Robie");
        wordDulicate.add("Helen");
        wordDulicate.add("Tom");
        wordDulicate.add("Troy");
        wordDulicate.add("Mika");
        wordDulicate.add("Tom");

        ArrayList<String> nonDupList = new ArrayList<String>();

        Iterator<String> dupIter = wordDulicate.iterator();
        while(dupIter.hasNext())
        {
        String dupWord = dupIter.next();
        if(nonDupList.contains(dupWord))
        {
            dupIter.remove();
        }else
        {
            nonDupList.add(dupWord);
        }
        }
      System.out.println(nonDupList);

输出:

[Tom, Jones, Sam, Jamie, Robie, Helen, Troy, Mika]
于 2012-10-02T19:50:03.097 回答
2

派对似乎有点晚了,但只有一个数组列表,

这是没有哈希集的代码......

strings = new ArrayList<>();
    strings.add("a1");
    strings.add("a2");
    strings.add("ss");
    strings.add("asd");
    strings.add("asd");
    strings.add("a1");
    strings.add("ss");
    strings.add("ss");
    System.out.println(Collections.frequency(strings, "ss"));
    for(int i = 0; i< strings.size();i++){
        int frequency = Collections.frequency(strings, strings.get(i));
        if(frequency>1){
            String string = strings.get(i);
            for(int j = 1 ; j < frequency ; j++)
                strings.remove(string);
        }
    }
    System.out.println(Collections.frequency(strings, "ss"));
    System.out.println(strings);
于 2015-03-05T04:28:51.490 回答
1
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ArrayListDuplicateRemove {

    public static void main(String[] args) {


       List<String> lst = new LinkedList<String>();
        lst.add("ABC");
        lst.add("ABC");
        lst.add("ABCD");
        lst.add("ABCD");
        lst.add("ABCE");

        System.out.println("Duplicates List "+lst);

        Object[] st = lst.toArray();
          for (Object s : st) {
            if (lst.indexOf(s) != lst.lastIndexOf(s)) {
                lst.remove(lst.lastIndexOf(s));
             }
          }

        System.out.println("Distinct List "+lst);

    }

}
于 2015-03-27T09:17:54.940 回答
1
package arraylist;

import java.util.ArrayList;
import java.util.Iterator;

class Student{
    String name;
    String dob;
    public Student(String name, String dob) {
        super();
        this.name = name;
        this.dob = dob;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDob() {
        return dob;
    }
    public void setDob(String dob) {
        this.dob = dob;
    }

    public String toString() {
        return name+"\t"+dob;
    }
}

public class RemoveDuplicate {

    public static void main(String[] args) {

        ArrayList<Student> list=new ArrayList<Student>();
        list.add(new Student("prakash", "89"));
        list.add(new Student("prakash", "89"));
        list.add(new Student("saurabh", "79"));
        list.add(new Student("saurabh", "79"));

        Iterator<Student> it=list.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }

        int size=list.size();
        for(int i=0;i<size-1;i++){
            for(int j=i+1;j<size-1;j++){
                if(list.get(i).getName().equals(list.get(j).getName())){
                    list.remove(j);
                }
            }
        }
            System.out.println(list);
    }
}
于 2015-08-15T08:47:35.697 回答
0
 ArrayList l1 = new ArrayList();
ArrayList l2 = new ArrayList();

Iterator iterator = l1.iterator();

    while (iterator.hasNext())
    {
        YourClass o = (YourClass) iterador.next();
        if(!l2.contains(o)) l2.add(o);
    }

l1 是原始列表,l2 是没有重复项的列表(确保 YourClass 具有根据您要代表相等的内容的 equals 方法)

于 2012-10-02T19:51:59.230 回答
0
ArrayList<Integer> al = new ArrayList<Integer>();

Scanner sc = new Scanner(System.in);
for(int i=0;true;i++){
    System.out.println("Enter your Element:");
     String s1= sc.nextLine();
     if(s1.equals("Exit")){
         break;
     }
     int x1= Integer.parseInt(s1);
     al.add(x1);

     for(int j=0;j<al.size();j++){
         for(int k=j+1;k<al.size();k++){

             int temp;
             if(al.get(j).intValue()>al.get(k).intValue()){
                 temp =al.get(j).intValue();
                 al.set(j, al.get(k).intValue());
                 al.set(k, temp);

             }//if
         }//for
     }//for
}//for
System.out.println(al);
于 2015-09-09T16:42:01.160 回答