0

so this method performs daily checks that adds one day to the displayedDays in order to determine the freshness of an item, so the method adds a day then check if it's rotten by calling the method isRotten() and if it's rotten it removes it the array

    for (int i = 0; i < numItems; i++) {
        items[i].displayedDays++;
    }

    for (int i = 0; i < numItems;) {

        if (items[i].isRotten()) {
            if (removeItem(i)) {
                if (i > 0) {
                    i--;
                }
                continue;
            }
        }

        i++;
    }

this is also another method that uses the same loop and if's so this method is supposed to remove the sweets from the array ( the array has two types of items bread and sweets)

    double totalSweetsPrice = 0;

    int count = numItems;

    for (int i = 0; i < count;) {
        Item item = items[i];

        if (item instanceof Sweet) {
            totalSweetsPrice += item.getPrice();
            if (removeItem(i)) {
                if (i > 0) {
                    i--;
                }
                continue;
            }
        }
        i++;
    }

and I don't understand the middle part and was hoping that there is a different loop or something whilst getting the same result

this was how a wrote the daily check method

    for (int i = 0; i < numItems; i++) {
    items[i].displayDays++ ;
    if(items[i].isRotten())
    removeItem(i); }

and the output was wrong

  • What exactly don't you understand? What behavior are you seeing that you want to understand and what about that behavior would you like to change with a different loop? – lolynns Dec 05 '19 at 20:38
  • one question, why doing `i--` (or `i++` outside the `for` statement)? Second, what should be different, despite checking the condition to check if an item should be removed or not? – user85421 Dec 05 '19 at 20:50
  • after-edit: your code is wrong since after removing item at index `i`, `i` is incremented, that is, following item , originally at `i+1` is now at `i`, but `i` is incremented so that item is not tested. EITHER do `i--` OR move `i++` out of `for` to end of loop (will be skipped by `continue`) – user85421 Dec 05 '19 at 20:56
  • that's the problem I found this method and pasted it in my code and it workes perfectly and I cannot understand why and how he thought about doing the for loop like that – dumblegend Dec 05 '19 at 20:57
  • That method you found is doing a messy song and dance to control for the fact that you’re editing the contents of an array while concurrently iterating through it. I would instead recommend a while loop, I gave an example below. – lolynns Dec 05 '19 at 21:51

3 Answers3

1

Most of the code stems from the fact you change the elements positions in an array you're enumerating. A different kind of data structure, more suited to the removal of elements, such as a linked list, would simplify greatly your code and would be more efficient.

FooBar
  • 132
  • 1
  • 9
0

As FooBar said, you can’t both iterate an ArrayList and remove items from that same ArrayList. So you need to separate the conditions. For example:

for (int i = 0; i < items.size(); i++) {
    items.get(i).displayedDays++;
}

int i = 0:
while (i < items.size()) {
    if(items.get(i).isRotten()) {
        items.remove(i);
        // don’t increment our counter here because now 
        // we’ve changed what object exists in the ArrayList 
        // at this index position
    } else {
        // only increment our counter if the item in that index
        // is good, so we can now move on to check the next item
        i++;
    }
}
lolynns
  • 339
  • 1
  • 8
  • @dumblegend you’re welcome! If this answers your question, please mark it as your accepted answer :) – lolynns Dec 06 '19 at 00:09
0

Why not use ArrayList.removeAll(Collection c)? This way, you can create a Collection, such as an ArrayList, iterate through the original list, add all rotten items to your new collection, then call removeAll on the original ArrayList after iterating through the entire list? That way, you don't have to worry about modifying the list while you're iterating through it.