为什么这段代码不从 ArrayList 中删除所有奇数? [英] Why doesn't this code remove all odd integers from the ArrayList?

查看:19
本文介绍了为什么这段代码不从 ArrayList 中删除所有奇数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 10 个非零正整数的 ArrayList.

0: 11031:7112:1993:15274:17455:15306:9847:7988:9279: 1986

当我运行这个

for(int i = 0; i 

列表更改为

0: 7111:15272:15303:9844:7985:1986

l1 是 UnorderedArrayList 的对象,它扩展了 ArrayListClass 实现了ArrayListADT(点击进入 pastebin)>

retrieveAt(index)removeAt(index) 是 ArrayListClass 的方法.

解决方案

在使用索引进行迭代时删除索引处的值会导致元素被跳过.例如,如果我们删除索引 0 处的元素,则列表变为:

0: 7111: 199 <--- 要检查的下一个元素2:15273:17454:15305:9846:7987:9278: 1986

对于循环的下一次迭代,索引现在增加到 1,因此新的第一个元素(711,它是 new 位置 0) 被跳过.此模式对其余值重复.

一个选项是使用 迭代器模式,正如所有 Java 所假定的Collection 类(例如 ArrayList).使用这种风格,您可以按如下方式重写循环:

for (Iterator it = l1.iterator(); it.hasNext(); ) {int temp = it.next();int temp2 = 温度 % 2;如果(温度 2 == 1){it.remove();}}

<小时>

phatfingers 的回答所述,首先从最高值开始迭代可以解决这个问题.循环将是相同的,但它会从最后一个索引 (l1.length - 1) 开始并继续直到到达 0.因此,索引将是 [9, 8, ..., 0] 而不是原始的 [0, 1, ..., 9].此解决方案的代码是:

for (int i = l1.length - 1; i >= 0; i--) {int temp = l1.retrieveAt(i);int temp2 = 温度 % 2;如果(温度 2 == 1){l1.removeAt(i);}}

I have an ArrayList of 10 positive nonzero integers.

0: 1103
1: 711
2: 199
3: 1527
4: 1745
5: 1530
6: 984
7: 798
8: 927
9: 1986

When I run this

for(int i = 0; i < l1.length; i++){
        int temp = l1.retrieveAt(i);
        int temp2 = temp % 2;
        if(temp2 == 1){
            l1.removeAt(i);
        }
    }

The list changes to

0: 711
1: 1527
2: 1530
3: 984
4: 798
5: 1986

l1 is an object of UnorderedArrayList which extends ArrayListClass which implements ArrayListADT (click for pastebin)

retrieveAt(index) and removeAt(index) are methods of ArrayListClass.

解决方案

Removing values at an index while iterating using that index can cause elements to be skipped. For example, if we remove the element at index 0, the list becomes:

0: 711
1: 199      <--- next element to check
2: 1527
3: 1745
4: 1530
5: 984
6: 798
7: 927
8: 1986

The index is now incremented to 1 for the next iteration of the loop, so the new first element (711, which is the element at the new position 0) is skipped. This pattern repeats for the rest of the values.

An option is to use the Iterator Pattern, as supposed by all Java Collection classes (such as ArrayList). Using this style, you can rewrite your loop as follows:

for (Iterator<Integer> it = l1.iterator(); it.hasNext(); ) {
    int temp = it.next();
    int temp2 = temp % 2;
    if (temp2 == 1) {
        it.remove();
    }
}


As stated in phatfingers's answer, iterating from the highest value first would remove this problem. The loop would be the same, but it would start at the last index (l1.length - 1) and continue until it reaches 0. Thus, the indices would be [9, 8, ..., 0] rather than the original [0, 1, ..., 9]. The code for this solution would be:

for (int i = l1.length - 1; i >= 0; i--) {
    int temp = l1.retrieveAt(i);
    int temp2 = temp % 2;
    if(temp2 == 1){
        l1.removeAt(i);
    }
}

这篇关于为什么这段代码不从 ArrayList 中删除所有奇数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆