无法删除ArrayList中的项目

时间:2014-11-22 03:41:41

标签: java arraylist duplicates

该程序的功能是删除Arraylist<Character>的重复项。在静态方法removeDuplicate()中,我首先将重复项的位置分配到LinkedList,然后通过在ArrayList中获取其位置来删除LinkedList中的重复项。如果输入a,a,a,a输入,则会看到不删除副本。谁能帮我?

import java.util.Scanner;
import java.util.LinkedList;
import java.util.ArrayList;
public class Duplicate2
{
    public static void main(String[] args)
    {
        ArrayList <Character> list = new ArrayList<> (2);
        Character y = 'A';
        Scanner kb = new Scanner (System.in);
        System.out.println("Enter 4 characters");
        for(int n = 0; n <4; n++)
        {
            y = kb.next().charAt(0); // no matter how many character in a line, only the first is kept.
            list.add(y);
        }
        System.out.println("Stop entering input.");
        Duplicate2.removeDuplicate(list);
        for (int n = 0; n < list.size(); n++)
            System.out.println(list.get(n));
    }

    public static void removeDuplicate (ArrayList <Character> data)
    {
        int k = data.size ();
        int t = 0;
        LinkedList <Integer> duplicatePosition = new LinkedList<>();
        ArrayList <Character> copyList = new ArrayList <> (2);
        for (int n = 0; n < data.size(); n ++)
        {
            Character x = data.get(n);
            if (copyList.isEmpty())
                copyList.add(x);
            else
            {
                for (int m = 0; m <copyList.size(); m++)
                {
                    if (x.equals(copyList.get(m)))
                    {
                        duplicatePosition.add(n);
                        t++;
                        break;
                        //data.remove(n);
                    }
                    else
                    {
                        copyList.add(x);
                        break;
                    }
                }
            }
        }
        System.out.println("Position of duplicate:");
        for (int n = 0; n < duplicatePosition.size(); n++)
            System.out.println(duplicatePosition.get(n));
        System.out.println("Start to remove duplicates");
        for (int n = 0; n< duplicatePosition.size(); n++)
        {
            System.out.println("Position " + duplicatePosition.get(n) + " would be removed.");
            data.remove(duplicatePosition.get(n));
            for (int m = 0; m < data.size(); m ++)
                System.out.println(data.get(m));
            if ((n+1)<duplicatePosition.size())
                duplicatePosition.set(n+1, duplicatePosition.get(n+1) -1);
        }
    }


}

2 个答案:

答案 0 :(得分:0)

这是您尝试删除元素的循环:

    for (int n = 0; n< duplicatePosition.size(); n++)
    {
        System.out.println("Position " + duplicatePosition.get(n) + " would be removed.");
        data.remove(duplicatePosition.get(n));
        ...
    }

问题在于,每次从duplicatePosition.get(n)移除data个元素时,都会移动以下所有元素(即原始(duplicatePosition.get(n)+1)&#39 ; th元素成为新的duplicatePosition.get(n)&#39;元素。

因此,例如,如果duplicatePosition包含1,2,3并且您调用data.remove(duplicatePosition.get(0)),则从数据中删除索引为1的元素,并在索引为2的元素的索引中删除数据将更改为1.因此data.remove(duplicatePosition.get(1))不会删除它。

要考虑到这一点,您必须计算已删除的元素,并从要删除的索引中减去计数:

    int removed = 0;
    for (int n = 0; n< duplicatePosition.size(); n++)
    {
        System.out.println("Position " + (duplicatePosition.get(n)-removed) + " would be removed.");
        data.remove(duplicatePosition.get(n)-removed);
        removed++;
    }

答案 1 :(得分:0)

我会采取不同的方法:

    // your original list is declared as 
    // List<Character> lst = ...
    Set<Character> set = new HashSet<>();
    set.addAll(lst);
    lst = new ArrayList<Character>();
    lst.addAll(set);
    // now lst doesn't contain duplicates