从ArrayList中删除另一个ArrayList中的元素

时间:2017-12-27 04:50:13

标签: java arrays arraylist collections

我无法从Arraylist中删除元素集合。 .remove()不适用于这种情况。

我有以下名为uncategorizedList的arrayList,其中包含我使用以下元素集合添加的一些信息。

List<ExpenseList> uncategorizedList = new ArrayList<>();
uncategorizedList.add(new ExpenseList("11/30/2017", "check deposit", 230.32));
uncategorizedList.add(new ExpenseList("09/12/2017", "cash deposit", 340.75));
uncategorizedList.add(new ExpenseList("08/01/2017", "edeposit", 30.01));
uncategorizedList.add(new ExpenseList("05/18/2017", "stephan", 1220.89));
uncategorizedList.add(new ExpenseList("03/02/2017", "bob", 20.50));

下一步是获取String []收入并查看uncategorizedList是否包含String []收入中的任何单词。如果是,我将日期,描述和价格添加到incomeList。代码完美无缺。

String[] income = {"edeposit", "stephan", "check deposit", "cash deposit"};
List<ExpenseList> incomeList = new ArrayList<>();

for (ExpenseList expense : uncategorizedList) {
     for(int i=0; i < income.length; i++) {
        if (expense.getDescription().toLowerCase().contains(income[i])) {
          incomeList.add(new ExpenseList(expense.getDate(),expense.getDescription(), expense.getPrice()));


                    }
                }
            }

我遇到的问题是如何删除添加到incomeList的uncategorizedList中的元素。正如我之前所说,.remove()不起作用。在uncategorizedList中唯一应该留下的是03/02/2017, bob, 20.50

4 个答案:

答案 0 :(得分:0)

你必须做listName.remove(xx)之类的事情 将该位置作为参数传递,例如

如果您要使用onLongClicks删除,您将获得这样的位置,

listName.get(position)
listName.remove(position)

答案 1 :(得分:0)

实际上,你使用迭代器的方式是错误的。当您在uncategorizedList上使用包含ExpenseList列表的Iterator时,iterate.next()将为您提供ExpenseList而不是String。因此,如果适合您,请尝试使用以下代码:

for (Iterator<ExpenseList> iterate = uncategorizedList.iterator(); iterate.hasNext();) {
   for(int i=0; i < income.length; i++) {
        ExpenseList expense = iterate.next();
        if (expense.getDescription().toLowerCase().contains(income[i])) {
            incomeList.add(new ExpenseList(expense.getDate(),expense.getDescription(), expense.getPrice()));
            // Remove the current element from the iterator and the list.
            iterate.remove();
        }
    }
} 

答案 2 :(得分:0)

你可以通过改变你的循环顺序来做一些小技巧,如下所示 -

String[] income = {"edeposit", "stephan", "check deposit", "cash deposit"};
    List<ExpenseList> incomeList = new ArrayList<>();
    for (int i = 0; i < income.length; i++) {
        ExpenseList expenseList = new ExpenseList("", "", 0);
        for (ExpenseList expense : uncategorizedList) {
            if (expense.getDescription().toLowerCase().contains(income[i])) {
                incomeList.add(new ExpenseList(expense.getDate(),expense.getDescription(), expense.getPrice()));
                expenseList = expense;
            }
        }
        uncategorizedList.remove(expenseList);
    }

或者您可以将对象从 uncategorilizedList 复制到 incomeList removeAll incomeList uncategorilizedList

        for (ExpenseList expense : uncategorizedList) {
        for (int i = 0; i < income.length; i++) {
            if (expense.getDescription().toLowerCase().contains(income[i])) {
                incomeList.add(expense);
            }
        }
    }

     uncategorizedList.removeAll(incomeList);

希望它能解决你的问题:)

答案 3 :(得分:0)

通过使用String类的contains()方法和list接口的removeAll()方法,代码可以写成如下:

String income = "edeposit stephan check deposit cash deposit";
List<ExpenseList> incomeList = new ArrayList<>();

for (ExpenseList expense : uncategorizedList) {
    if (income.contains(expense.getDescription().toLowerCase())) {
        incomeList.add(expense);
    }
}

uncategorizedList.removeAll(incomeList);