ConcurrentModificationExecption

徘徊边缘 提交于 2019-12-12 03:35:29

问题


I have a normal Database call that gathers information from my database. i use these informations to create my objects (CallQueue) these objects are then added to a list and the list is then returned.

Suddenly i discovered that my originally code did not work as intended because i created dublicates and so now i am trying to void that any dublicates are being created! But there is a problem!

I am unable to loop through my list and check wether or not the object is already created!

Here is my code:

while (query.next()) {
    if (!queues.isEmpty()) {
        /*This gives the Execption->*/
        for (CallQueue callQueue : queues) {
            if (callQueue.getType().equals(query.getString("KØ"))) {
                double decimalTime = query.getDouble("TID");
                int hourOfDay = (int)Math.round(24 * decimalTime);
                int callAmount = query.getInteger("ANTAL_KALD");
                if (hourOfDay > 19) {
                    hourOfDay = 19;
                }
                callQueue.addCallsByTime(hourOfDay, callAmount);
            } else {
                String queueName = query.getString("Kø");
                if (!queueName.equalsIgnoreCase("PrivatOverflow")) {
                    CallQueue cq = new CallQueue(query.getString("KØ"));
                    double decimalTime = query.getDouble("TID");
                    int hourOfDay = (int)Math.round(24 * decimalTime); 
                    int callAmount = query.getInteger("ANTAL_KALD");
                    if (hourOfDay > 19) {
                        hourOfDay = 19;
                    }
                    cq.addCallsByTime(hourOfDay, callAmount);
                    queues.add(cq);
                }
            }
        }
    } else {
        String queueName = query.getString("Kø");
        if (!queueName.equalsIgnoreCase("PrivatOverflow")) {
            CallQueue cq = new CallQueue(query.getString("KØ"));
            double decimalTime = query.getDouble("TID");
            int hourOfDay = (int)Math.round(24 * decimalTime); 
            int callAmount = query.getInteger("ANTAL_KALD");
            if (hourOfDay > 19) {
                hourOfDay = 19;
            }
            cq.addCallsByTime(hourOfDay, callAmount);
            queues.add(cq);
        }
    }
}

for (CallQueue callQueue : queues) {
    System.out.println(callQueue.getType());
}
query.Close();
return queues;

The execption i get from this is:

Caused by: java.util.ConcurrentModificationException

ive tried to look up the execption at ConcurrentModificationException

Can anyone help me fix this problem?


回答1:


You're doing an add inside of your iteration. As per the spec, you're not allowed to modify the collection you're iterating over.

The classic solution is to make a copy of the collection first and iterate over that instead. Another solution is to not use the iterator (the short foreach notation is using it implicitly) but to manually iterate using an index.

for (int i=0; i<queues.size(); i++) {
    CallQueue callQueue = queues.get(i);

    ... code goes here

}

A better solution would be to use a Set instead of a list (unless the order is important for you). That does mean you would have to implement equals and hashcode properly.

Btw: I believe your code is flawed. You're iterating over your list, and if the item you encounter does not match, you add one at the end. Meaning that, if the item you're looking for is the xth one in the list, you will have added a new item x times. I seriously doubt that is what you need. If you do some refactoring, this will become immediately clear.




回答2:


Use set instead of List to avoid this problem. You can iterate on the same list that you want to modify.

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



来源:https://stackoverflow.com/questions/13609323/concurrentmodificationexecption

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!