简体   繁体   English

循环不遍历整个arraylist

[英]loop not iterating through entire arraylist

I'm having a problem where I am trying to iterate through an arraylist which stores restaurant objects but the loop only goes through 3 of the five elements. 我在尝试遍历存储餐厅对象的数组列表时遇到问题,但循环仅遍历五个元素中的三个。

I created a test method to illustrate: 我创建了一个测试方法来说明:

public void testLoop() {
        ArrayList<Eatery> test = new ArrayList<Eatery>();
        test = eateriesListDefault;
        for(Eatery e : test) {
            MyLog.e(TAG, "Name: " + e.getName());
        }
        for (int i = 0; i < eateriesListDefault.size(); i++) {
            MyLog.e(TAG, "Name " + test.get(i).getName());
            test.remove(i);
        }
        for(Eatery e : test) {
            MyLog.e(TAG, "Name " + e.getName());
        }
    }

Here test will have 5 eatery objects in it. 测试中将包含5个餐饮对象。 The first loop succesfully prints 5 of 5 names. 第一个循环成功打印出5个名字中的5个。 The second loop only removes 3 of the eateries and therefore the last loop prints two names. 第二个循环仅删除3个餐厅,因此最后一个循环将打印两个名称。

I have tried using 我尝试使用

for(Eatery e : eateriesListDefault) {
            MyLog.e(TAG, "Name: " + e.getName());
            test.remove(e);
}

in place of the second loop, however I get a concurrent access error. 代替第二个循环,但是出现并发访问错误。

Does anyone know what I am doing wrong? 有人知道我在做什么错吗?

You're calling eateriesListDefault.size() in each loop iteration. 您在每次循环迭代中都调用eateriesListDefault.size() At the same time, you're calling test.remove(i) which is shorting the array by one every iteration. 同时,您正在调用test.remove(i) ,它在每次迭代中将数组缩短一。 Your loop is essentially doing this: 您的循环实际上是在这样做:

  1. i = 0 size = 5 Keep going 我= 0大小= 5继续前进

  2. i = 1 size = 4 Keep going i = 1大小= 4继续前进

  3. i = 2 size = 3 Keep going i = 2大小= 3继续前进

  4. i = 3 size = 2 stop i = 3尺寸= 2挡

If you're goal was to print out the first element of the array then remove it, you could probably get there by this loop: 如果您的目标是打印出数组的第一个元素然后将其删除,则可以通过以下循环到达该位置:

while(!eateriesListDefault.isEmpty()) {
   MyLog.e(TAG, "Name " + test.get(0).getName());
   test.remove(0);
}

Your second loop removes elements from the array while your loop index continues to increase. 当循环索引继续增加时,第二个循环将从数组中删除元素。 On the fourth pass, your loop index is 3, but eateriesListDefault.size is 2, so the loop exits. 在第四遍,循环索引为3,但eateriesListDefault.size为2,因此循环退出。

Try this instead: 尝试以下方法:

for ( Iterator<Eatery> it = test.iterator(); it.hasNext(); ) {
   MyLog.e(...)
   it.remove();
}

The behavior of an iterator is unspecified if the list is modified during iteration in any way other than by calling Iterator.remove(). 如果在迭代过程中通过除调用Iterator.remove()之外的任何其他方式修改列表,则未指定迭代器的行为。 See the Iterator.remove() documentation . 请参阅Iterator.remove()文档

Also, you may want to make a copy of the default list. 另外,您可能需要复制默认列表。 Your "test =" statement is just making a new reference to the same list. 您的“ test =”语句只是在引用相同的列表。

test = new ArrayList<Eatery>( eateriesListDefault ); 

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

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