简体   繁体   English

迭代时从ArrayList中删除奇数元素

[英]Remove odd elements from ArrayList while iterating

I have an ArrayList of Strings and I'm trying to remove the odd elements of the ArrayList, ie list.remove(1), list.remove(3), list.remove(5) etc. 我有一个字符串的ArrayList,我试图删除ArrayList的奇数元素,即list.remove(1),list.remove(3),list.remove(5)等。

This is the code I'm attempting to use which throws up an IllegalStateException error: 这是我尝试使用的代码,它引发IllegalStateException错误:

int i = 0;
    for (Iterator<String> it = words.iterator(); it.hasNext(); )
    {
        if (i % 2 != 0 && it.hasNext())
        {
            it.remove();
        }
        i++;
    }

Is there a better (working) way to do this? 有没有更好的(可行的)方法来做到这一点?

int i = 0;
    for (Iterator<String> it = words.iterator(); it.hasNext(); )
    {
        it.next(); // Add this line in your code
        if (i % 2 != 0)
        {
            it.remove();
        }
        i++;
    }

You can try something like this to remove every second element starting from words[1] . 您可以尝试执行类似的操作,以删除words[1]开头的第二个元素。 No need to check whether the index is odd, when we remove an element we can just increment i and that will be the next odd number. 无需检查索引是否为奇数,当我们删除元素时,我们只需增加i ,它就是下一个奇数。

int i = 1;

while (i < words.size()) {
    words.remove(i++);
}

You need to clone this Array or copy that odd element into another array. 您需要克隆此数组或将该奇数元素复制到另一个数组。 During iterate time it was used same object so if you remove its index and state was changes. 在迭代期间,它使用了相同的对象,因此,如果删除它的索引并且状态是更改的。

int i = 0;
    List<String> list = new ArrayList<String>();
    List<String> words = new ArrayList<String>();
    for (String word:words)
    {
        if (i % 2 != 0)
        {

            //it.remove();
            list.add(word);
        }

        i++;
    }
    words.removeAll(list);

now just remove this all by passing this list to your words list object 现在,只需将此列表传递给您的单词列表对象即可将其删除

words.removeAll(list);

I wouldn't use an iterator, because you don't need to examine the elements. 我不会使用迭代器,因为您不需要检查元素。 Just use List.remove(index) : 只需使用List.remove(index)

for (int i = words.size() - 1; i >=0; i--) {
    if (i % 2 == 1) {
        words.remove(i);
    {
}

Note that you must count down, not up with this approach because removing an element shuffles e everything after to the left 请注意,您必须倒数,而不是使用这种方法,因为删除元素会拖曳左侧的所有内容

If your list is immutable (explains the exception) make a copy first: 如果您的列表是不可变的(解释异常),请首先进行复制:

words = new ArrayList(words);

Just use this. 只是使用这个。

for (int i = 1; i < list.size(); i++) {
    list.remove(i);
}

The below also works fine 下面也很好

public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(1, 2, 3,
                4, 5, 6, 7, 8, 9));

        for (int i = 0; i < list.size(); i++) {
            System.out.println(i);
            list.remove(i);
        }
        System.out.println(list);
    }

yielding 屈服

[2, 4, 6, 8]

You need to use next() and then call remove() --- 您需要使用next(),然后调用remove()-

    int counter = 0;
    for (final Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
        iterator.next();
        if (++counter % 2 != 0) {
            iterator.remove();
        }
    }
public static List<Integer> removeImpairElements(List<Integer> integers){
int j = 0; 
for(int i = 0 ; i < integers.size(); i++){
  if( i % 2 == 0){
    integers.set(j, integers.get(i));
    j++;
  }
}
int half = integers.size()%2==0 ? integers.size()/2 : integers.size()/2 + 1;  
integers.subList(half , integers.size()).clear();
return integers;

} }

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

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