简体   繁体   English

从 ArrayList 中删除元素 - 代码正在跳过元素

[英]Remove elements from ArrayList - Code is skipping elements

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.如果 i.remove() 被调用,迭代器似乎仍然会跳过元素,我理解这正是迭代器要防止的。

Could someone point out where I am going wrong?有人能指出我哪里出错了吗?

Thanks谢谢

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.如果您想访问索引,则应该使用ListIterator而不是普通的Iterator ,并且即使在删除元素时也要保持正确更新。 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.使用listIterator()方法获取它,在循环中,使用nextIndex()获取索引,使用next()获取下一个 object,按此顺序。

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.只需单独使用 object,根据需要传递它,或者同时传递索引和元素。 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();
       // ...
    }
}

Don't mix list.indexOf and the iterator.不要混合list.indexOf和迭代器。 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.从元素中获取字符串(我假设Content object 有字符串,对吗?)并使用它。

while (i.hasNext()) {

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

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

Is this line int index = list.indexOf(element);这行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();也许您应该更改Content以便它可以返回它自己的段落String paragraph = element.getParagraph(); And if you are iterating an ArrayList from first to last, why do you need to call indexOf every round?如果您从头到尾迭代 ArrayList,为什么需要在每一轮中调用indexOf

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM