当前位置:编程学习 > JAVA >>

java.util.ConcurrentModificationException的解决办法

 
使用Iterator来迭代的代码如下:
public String findByEvent() throws Exception { 
        try { 
            eventName = new String(eventName.getBytes("iso-8859-1"), "UTF-8"); 
            listGroupInfo = this.groupInfoService.findByEvent(eventName); 
                        Iterator<GroupInfo> iter = listGroupInfo.iterator(); 
            if (listGroupInfo.size() != 0) { 
                GroupInfo groupInfo = iter.next(); 
                for (int i = 0; i < listGroupInfo.size() - 1; i++) { 
                    for (int j = listGroupInfo.size() - 1; j > i; j--) { 
                        if (listGroupInfo.get(j).getGroupNumber().equals( 
                                listGroupInfo.get(i).getGroupNumber())) { 
                            listGroupInfo.remove(j); 
                        } 
                    } 
                } 
 
            } 
        } catch (Exception e) { 
            // TODO: handle exception 
            e.printStackTrace(); 
            return INPUT; 
        } 
        return SUCCESS; 
 
    }
上面的代码运行正常!
本次使用的源代码如下:
 
public String findUnit() throws Exception { 
        try { 
             
            Map session = ActionContext.getContext().getSession(); 
            String ids = session.get("userId").toString(); 
            id = Long.parseLong(ids); 
            listAthlete = this.athleteService.findByUserId(id); 
            listUnitInfo = this.unitInfoService.findByUserId(id); 
            for(UnitInfo unitInfo :listUnitInfo){ 
                for(Athlete athlete:listAthlete){ 
                    if(athlete.getUnitGroup().getUnitInfo().getName().equals(unitInfo.getName())){ 
                        System.out.println("移除:"+unitInfo.getName()); 
                        listAthlete.remove(athlete); 
                        listUnitInfo.remove(unitInfo); 
                         
                    } 
                } 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
            System.out.print(e); 
            return INPUT; 
        } 
        return SUCCESS; 
    }

使用的是foreach,因为大家都知道使用foreach为我们带来了极大的方便,所以考虑使用foreach也是正常的思路。但是,使用的时候,却产生了如下的异常。
 
java.util.ConcurrentModificationException
java.util.ConcurrentModificationException 
    at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372) 
    at java.util.AbstractList$Itr.next(AbstractList.java:343) 
    at com.yaxing.action.AthleteAction.findUnit(AthleteAction.java:398) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254) 
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263) 
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) 
    at com.opensymphony.xwork2.intercept

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,