使用的foreach例外occours从ArrayList中删除对象只当列表规模以上2 [英] removing object from arraylist using foreach exception occours only when the list size above 2

查看:199
本文介绍了使用的foreach例外occours从ArrayList中删除对象只当列表规模以上2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类检查使用foreach循环从ArrayList中删除对象的

I have the below class for checking removing of object from arraylist by using foreach loop

import java.util.ArrayList;

    public class CheckArrayList {
        /**
         * @return the a
         */
        public int getA() {
            return a;
        }

        /**
         * @param a
         *            the a to set
         */
        public void setA(int a) {
            this.a = a;
        }

        /**
         * @return the b
         */
        public int getB() {
            return b;
        }

        /**
         * @param b
         *            the b to set
         */
        public void setB(int b) {
            this.b = b;
        }

        int a;
        int b;

        public static void main(String[] args) {
            try {
                CheckArrayList checkArrayList1 = new CheckArrayList();
                checkArrayList1.setA(10);
                checkArrayList1.setB(20);

                CheckArrayList checkArrayList2 = new CheckArrayList();
                checkArrayList2.setA(30);
                checkArrayList2.setB(40);

                ArrayList<CheckArrayList> aList = new ArrayList<CheckArrayList>();

                aList.add(checkArrayList1);
                aList.add(checkArrayList2);

                System.out.println("size of list before iterate = " + aList.size());
                for (CheckArrayList checkArrayList : aList) {
                    aList.remove(checkArrayList);
                    System.out.println("inside loop");
                }
                System.out.println("size of list after iterate = " + aList.size());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

虽然上面的操作做它没有显示任何异常,但它涉及的循环中只有一次。

while doing above operation it is not showing any exception but it comes inside the loop only once.

如果我添加一个对象北京时间并做出相同的操作 java.util.ConcurrentModificationException

if I add one more object to ist and doing same operation it showing java.util.ConcurrentModificationException

import java.util.ArrayList;

public class CheckArrayList {
    /**
     * @return the a
     */
    public int getA() {
        return a;
    }

    /**
     * @param a
     *            the a to set
     */
    public void setA(int a) {
        this.a = a;
    }

    /**
     * @return the b
     */
    public int getB() {
        return b;
    }

    /**
     * @param b
     *            the b to set
     */
    public void setB(int b) {
        this.b = b;
    }

    int a;
    int b;

    public static void main(String[] args) {
        try {
            CheckArrayList checkArrayList1 = new CheckArrayList();
            checkArrayList1.setA(10);
            checkArrayList1.setB(20);

            CheckArrayList checkArrayList2 = new CheckArrayList();
            checkArrayList2.setA(30);
            checkArrayList2.setB(40);

            CheckArrayList checkArrayList3 = new CheckArrayList();
            checkArrayList3.setA(30);
            checkArrayList3.setB(40);

            ArrayList<CheckArrayList> aList = new ArrayList<CheckArrayList>();

            aList.add(checkArrayList1);
            aList.add(checkArrayList2);
            aList.add(checkArrayList3);

            System.out.println("size of list before iterate = " + aList.size());
            for (CheckArrayList checkArrayList : aList) {
                aList.remove(checkArrayList);
                System.out.println("inside loop");
            }
            System.out.println("size of list after iterate = " + aList.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

为什么没有显示异常,如果arralist大小等于2?

推荐答案

要回答为什么它不就得看什么的每个循环实际执行 2的元素抛出一个错误(输入内容:myList中)编译成这样的:

To answer why it doesn't throw an error on 2 elements you have to look at what the for each loop actually does.for(Type something:myList) compiles to something like:

Iterator<Type> iter = myList.iterator();
while(iter.hasNext()){
     Type something= iter.next()
     //your code
}

现在看看源$ C ​​$ C为的ArrayList 实际实现。在code为规则hasNext()是:

Now look at the source code for ArrayList for the actual implementation. The code for hasNext() is:

return cursor != size; //cursor is the current position in the list

所以,当你删除元素的循环中,尺寸现在是1。当它看起来,看看是否有更多的元素,它将返回false,因为它认为,是在ArrayList的结束,并停止执行在这一点上环

So when you remove the element in the loop, the size is now 1. When it looks to see if there are more elements it will return false because it thinks that is at the end of the ArrayList, and stops executing the loop at this point.

现在看code为的next()

public E next() {
        checkForComodification();
        //more code
}

这是在哪里进行修改的实际检查时,所以当大小为3,将执行第二次迭代,当它调用的next()来获得的第二个元素,它会抛出一个异常,因为你已经修改了ArrayList中的迭代器之外。

This is where the actual checking for modification occurs, so when size is 3, it will execute the second iteration and when it calls next() to get the second element, it will thrown an exception because you have modified the ArrayList outside of the iterator.

这篇关于使用的foreach例外occours从ArrayList中删除对象只当列表规模以上2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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