并发修改异常而迭代的ArrayList(Concurrent modification Except

2019-07-04 17:07发布

在并发修改错误的排序方法的结果时,我使用温度= iterator.next()。 能否请你帮我解决了并发修改错误。 我给整个类的代码,但我只是想完成的排序方法。 在此先感谢您的帮助。

我要所有的ArrayList中的数组进行排序。

    package HashSet;

            import java.io.InputStream;
            import java.util.ArrayList;
            import java.util.Collections;
            import java.util.ListIterator;
        import java.util.Scanner;

        public class PhoneBook {
            int capacity = 10;
            private ArrayList<PhoneBookEntry>[] buckets;

            public PhoneBook() {
                this(10);
                load();
            }

            public PhoneBook(int size) {
                capacity = size;
                buckets = new ArrayList[size];
                for (int i = 0; i < buckets.length; i++)
                    buckets[i] = new ArrayList<PhoneBookEntry>();
            }

            public int getSize() {
                int tot = 0;
                for (ArrayList<PhoneBookEntry> x : buckets)
                    tot += x.size();
                return tot;
            }

            public boolean add(PhoneBookEntry entry) {
                if (contains(entry))
                    return false;
                int x = Math.abs(entry.hashCode());
                buckets[x % buckets.length].add(entry);
                return true;
            }

            public void load() {
                InputStream is = getClass().getClassLoader().getResourceAsStream(
                        "phone.txt");
                Scanner scan = new Scanner(is);
                while (scan.hasNext())
                    add(new PhoneBookEntry(scan.next(), scan.nextInt()));
                scan.close();
            }

            public void bucketSize() {
                for (int i = 0; i < buckets.length; i++)
                    System.out.println(i + "    " + buckets[i].size());
            }

            public boolean contains(PhoneBookEntry word) {
                int x = Math.abs(word.hashCode());
                return buckets[x % buckets.length].contains(word);
            }

            public int getCapacity() {
                return capacity;
            }

            public int getLongestList() {
                int numb = 0;
                for (ArrayList<PhoneBookEntry> x : buckets)
                    if (x.size() > numb)
                        numb = x.size();
                return numb;
            }

            public void display() {
                for (ArrayList<PhoneBookEntry> x : buckets)
                    System.out.println(x);
            }

            public int getNumberOfNulls() {
                int numb = 0;
                for (ArrayList<PhoneBookEntry> x : buckets)
                    if (x.size() == 0)
                        numb++;
                return numb;
            }

            public String lookup(String name) {
                String numb = name + "'s number not found";
                for (ArrayList<PhoneBookEntry> x : buckets)
                    for (int i = 0; i < x.size(); i++)
                        if (x.get(i).getN().equals(name))
                            numb = name + "'s" + " number is " + x.get(i).getNr();
                return numb;
            }

            public int internalLookUp(String name) {
                int numb = 0;
                for (ArrayList<PhoneBookEntry> x : buckets)
                    for (int i = 0; i < x.size(); i++)
                        if (x.get(i).getN().equals(name))
                            numb = x.get(i).getNr();
                return numb;
            }

            public void sort() {
                String temp = "";
                ArrayList<String> list = new ArrayList<String>();
                ListIterator<String> iterator = list.listIterator();
                final ArrayList<PhoneBookEntry>[] data = buckets.clone();
                for (ArrayList<PhoneBookEntry> x : buckets) {
                    for (int i = 0; i < x.size(); i++) {
                        list.add(x.get(i).getN());
                    }
                    Collections.sort(list);
                    for (int b = 0; b < x.size(); b++) {
                        temp = iterator.next(); //error line
                        x.get(b).setN(temp);
                        x.get(b).setNr(internalLookUp(temp));
                    }
                }
            }

            public static void main(String[] args) {
                PhoneBook phone = new PhoneBook();
                phone.display();
                System.out.println();
                System.out.println("Capacity is " + phone.getCapacity());
                System.out.println();
                System.out.println("Size is " + phone.getSize());
                System.out.println();
                System.out.println("Get longest list " + phone.getLongestList());
                System.out.println();
                System.out.println("Number of Nulls " + phone.getNumberOfNulls());
                System.out.println();
                System.out.println(phone.lookup("Fish"));
                phone.sort();
            }
        }

Answer 1:

它看起来像你的问题是,你创建一个迭代器列表,然后修改列表(添加+排序),然后尝试使用迭代器。

如果改为创建之后这个迭代器,它应该工作。

例如。

            for (ArrayList<PhoneBookEntry> x : buckets) {
                for (int i = 0; i < x.size(); i++) {
                    list.add(x.get(i).getN());
                }
                Collections.sort(list);
                ListIterator<String> iterator = list.listIterator(); // Iterator created here
                for (int b = 0; b < x.size(); b++) {
                    temp = iterator.next(); //error line
                    x.get(b).setN(temp);
                    x.get(b).setNr(internalLookUp(temp));
                }


Answer 2:

该缺陷是,你遍历列表 ,而你添加到它。 你不应该这样做。



Answer 3:

下面是的Javadoc ConcurrentModificationException

http://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html

如果一个线程直接修改的集合,同时它遍历集合与快速失败迭代器,迭代器都将抛出此异常。

至于其他的海报说,你应该做的所有排序和前或使用后,迭代器加入。 您可以迭代过程中删除元素,但必须使用Iterator.remove()以防止再次获得相同的异常。



文章来源: Concurrent modification Exception while iterating over ArrayList