插入arrayList中的值时出现java.util.ConcurrentModificationException

时间:2015-08-12 07:02:04

标签: java

javax.faces.FacesException: #{scheduleBean.insert()}: java.util.ConcurrentModificationException
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: javax.faces.el.EvaluationException: java.util.ConcurrentModificationException

代码:

 public void insert(){

       for(ScheduleMaster v:arrayList1){

           arrayList1.add(mm.i, v); 
       }           
       List<ScheduleMaster>temp1=new ArrayList<ScheduleMaster>();
       temp1=arrayList1;
       System.out.println("Coming Insert");
       Iterator<ScheduleMaster>itr=temp1.iterator();
         while(itr.hasNext()){
             ScheduleMaster s=itr.next();
             s.setServiceCode(serviceCode);
             s.setServiceName(serviceName);
             s.setVessalCode(vessalCode);
             s.setVessalName(vessalName);
             s.setVoyage(voyage);
             s.setPortCode(portCode);
             s.setPortName(portName);
             s.setTerminalCode(terminalCode);
             s.setTerminalName(terminalName);
             s.setEta(eta);
             s.setEtaTimes(etaTimes);
             s.setEtd(etd);
             s.setEtdTimes(etdTimes);
             s.setBound(bound);
             s.setAta(ata);
             s.setAtaTimes(ataTimes);
             s.setAtd(atd);
             s.setAtdTimes(atdTimes);
             arrayList1.add(s);                        

       } 

   }    

1 个答案:

答案 0 :(得分:2)

temp1arrayList1是对同一对象的引用。而且你不能迭代一个对象并在迭代时改变它。

因此,您必须克隆该对象,然后您可以遍历克隆并更改原始对象。

  List<ScheduleMaster>temp1=new ArrayList<ScheduleMaster>();
  for(ScheduleMaster v:arrayList1){

       arrayList1.add(mm.i, v); 
       temp1.add(mm.i, v); 
   }