-1

I'm having trouble deleting a collection of elements from an Arraylist. .remove() does not work for this situation.

I have the following arrayList called uncategorizedList that contains some information that I added with the following collection of elements.

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

the next step is to take String[] income and see if uncategorizedList contains any of the words in String[] income. If it does, I add the date, description, and price to incomeList. The code works perfectly.

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


                    }
                }
            }

What I'm having a problem with is how to delete the elements from uncategorizedList that were added to incomeList. As I stated earlier, .remove() does not work. The only thing that should be left in uncategorizedList is 03/02/2017, bob, 20.50

shmosel
  • 49,289
  • 6
  • 73
  • 138
Stephan
  • 19
  • 1
  • 9
  • you might need Iterator for that – johnII Dec 27 '17 at 04:52
  • Possible duplicate of [Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop](https://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re) – VPK Dec 27 '17 at 04:52
  • @VPK I added the iterator and it's still not removing what I want.. check the edited post – Stephan Dec 27 '17 at 05:07

4 Answers4

0

You have to do something like listName.remove(xx)
Pass in the position as a parameter e.g

If you are removing using onLongClicks you would get position like this,

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

Actually, the way you have used the Iterator is wrong. When you are using Iterator on uncategorizedList which has list of ExpenseList, iterate.next() will give you ExpenseList and not String. So try following code if it works for you:

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();
        }
    }
} 
VPK
  • 3,010
  • 1
  • 28
  • 35
  • This works!!! you're awesome... at first I got a java.util.NoSuchElementException error but all I had to do to fix that is take ExpenseList expense = iterate.next(); and place it inside the first for loop... – Stephan Dec 27 '17 at 05:30
  • I'm glad it did. Happy Coding!! – VPK Dec 27 '17 at 05:32
0

You can do a little trick here by changing your sequence of loop like the following-

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

or you could copy object from uncategorilizedList to incomeList and removeAll incomeList from 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);

Hope it solves your problems :)

Sohel0415
  • 9,523
  • 21
  • 30
0

By using contains() method of String class and removeAll() method of list interface the code can be written as follows:

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);
Nithin
  • 748
  • 1
  • 10
  • 27