0

What I am trying to do is to loop through a list of indices that refer to a paragraph somewhere. If there paragraph is too short, remove the index from the list.

This is the code I am using at the moment, after googling around, I tried to take the advice to use an Iterator, but still no luck.

    ArrayList<Content> list = anotherList;

    Iterator<Content> i = list.iterator();
    while (i.hasNext()) {

        Content element = i.next();
        int index = list.indexOf(element);

        String paragraph = getContent(index);

        if (paragraph.split(" ").length < minWordCount) {
            i.remove();
        } else {
            irrelevantFuction(index);
        }
    }

The iterator still seems to skip over elements if i.remove() is called, which I understood was exactly what an iterator is meant to prevent.

Could someone point out where I am going wrong?

Thanks

3 Answers 3

3

You should use a ListIterator instead of the normal Iterator if you want access to the index, and keep it updated properly even when elements are removed. Use the listIterator() method to get it and while in the loop, use the nextIndex() to get the index and next() to get the next object, in that order.

Also, don't use the index if you can help it. Just use the object on its own, passing that around as needed, or pass both the index and the element. Either way, don't use the index again for accessing the list.

ListIterator<Content> i = list.listIterator();
int count = 0;
while (i.hasNext()) {
    int index = i.nextIndex();
    Content element = i.next();
       // ...
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Don't mix list.indexOf and the iterator. This is what probably causing you problems. Get the string from element (I assume a Content object has the string, right?) and work with it.

while (i.hasNext()) {

    Content element = i.next();
    String paragraph = element.yourMethodToGetTheString() // Some method...

    if (paragraph.split(" ").length < minWordCount) {
        i.remove();
    } else {
        irrelevantFuction(index);
    }
}

Comments

1

Is this line int index = list.indexOf(element); returning the correct index (try printing it)? Maybe you should change Content so it can return it's own paragraph String paragraph = element.getParagraph(); And if you are iterating an ArrayList from first to last, why do you need to call indexOf every round?

Try:

    Iterator<Content> i = list.iterator();
    int count = 0;
    while (i.hasNext()) {

        Content element = i.next();

        String paragraph = element.getParagraph();

        if (paragraph.split(" ").length < minWordCount) {
           i.remove();
        } else {
         // fixed according to comment
         int index = count++;
         irrelevantFuction(index);
       }
   }

5 Comments

your second suggestion is a bit dangerous, incrementing the counter every time while removing elements from the list is exactly his problem.
You're right (In my head he was updating the index or something like that, I will fix the code in a second.
@MByD Done. I think this will reflect correct indexes. @Chris second suggestion is a even better way.
Why not simply irrelevantFunction(count++);?
Yeah, it's equivalent and shorter. But looking at my code I would say that the ListIterator approach is better. I mean, if it's there, use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.