#Can you remove an element in the middle in a LinkedList?

1 messages · Page 1 of 1 (latest)

next dagger
#

I checked the documentation on oracle and there doesn't seem to be a method that can do that so I wonder if there is a trick that I can somehow use to remove an elment? Here is my code:

        for (Egg egg : linkedEggs) {
            if (CollisionChecker(egg.getxCor(), egg.getyCor()) < 20) {
                linkedEggs.remove(egg);
            } else if (egg.yCorEgg > 400) {
                linkedEggs.remove();
            }
        }
heavy epochBOT
#

This post has been reserved for your question.

Hey @next dagger! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

surreal tide
#

either .remove(element) to remove that element from the list, or remove(int) to remove the element at that index from the list

#

i assume your code threw a ConcurrentModificationException, that's why you're asking, right?

humble osprey
#

Instead of a for loop you can use an Iterator or possibly a ListIterator, and a while loop on it.
They offer their own remove() method to remove the latest item seen, but also keep iterating over the rest of the items.