ConcurrentModificationException的? [英] ConcurrentModificationException?

查看:109
本文介绍了ConcurrentModificationException的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按开始时间排序时间跨度列表(用开始时间和结束时间表示为Time []数组)。我正在尝试使用以下嵌套循环:

I'm trying to order a list of time spans (represented as Time[] arrays with a start time and end time) by their start times. I'm trying to use the following nested loop to do so:

            for (Time[] span : workingList){
            Time[] compareTo = workingList.get(0);

                for (Time[] inSpan : workingList){
                    if (inSpan[0].before(compareTo[0])){
                    compareTo = inSpan;
                    }
                }
            workingList.remove(compareTo);
            toReturn.add(compareTo);
        }

    }

但抛出 java.util.ConcurrentModificationException 行(Time [] span:workingList)(顶部的那个)。我以前从未见过这个例外,有人可以向我解释它的含义和原因。

but it is throwing a java.util.ConcurrentModificationException at the line for (Time[] span : workingList) (the one on top). I've never seen this exception before, can someone please explain to me what it means and what causes it.

我也愿意接受更好的算法建议。

I'm also open to suggestions of better algorithms for this.

推荐答案

workingList.remove(compareTo);

您正在迭代修改集合。

你应该使用类似的东西:

You should use something like:

ListIterator<Time[]> it = workingList.listIterator();

while (it.hasNext()) {
  Time[] time = it.next();
  ..
  it.remove();
}

有些方法也没有使用列表迭代器,但这似乎更正确。

There are ways also without using the list iterator but this seems the more correct.

这篇关于ConcurrentModificationException的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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