也许这可能是一个重复的问题,对此我很抱歉。但我的问题没有解决。
当 foreach 循环中第二次执行时,下面的代码给了我这个异常(java.util.ConcurrentModificationException)。即使我使用迭代器删除了 arraylist 对象。
@RequestMapping("edit")
public ModelAndView editItemToInvoice(HttpSession session,@RequestParam("itemname")String itemname){
ArrayList<InvoiceEntities> show=(ArrayList<InvoiceEntities>)session.getAttribute("invoices");
if(show==null){
show=new ArrayList<InvoiceEntities>();
}
ArrayList<InvoiceEntities> edit=new ArrayList<InvoiceEntities>();
for(InvoiceEntities itemnam:show){
if(itemnam.getItemName().equals(itemname)){
int index=show.indexOf(itemnam);
edit.add(show.get(index));
Iterator<InvoiceEntities> iter = show.iterator();
while(iter.hasNext()){
InvoiceEntities getitem=iter.next();
if(getitem.getItemName().equals(itemname)){
iter.remove();
//break;
}
}
}
}
System.out.println(session.getAttribute("invoices"));
ModelAndView model=new ModelAndView();
session.setAttribute("invoices", show);
model.addObject("editobj",edit);
model.addObject("items",session.getAttribute("invoices"));
model.setViewName("jsp/Invoice");
return model;
}
Run Code Online (Sandbox Code Playgroud)
异常是 java.util.ConcurrentModificationException
SEVERE: Servlet.service() for servlet [spring-dispatcher] in context with
path [/Invoice] threw exception [Request processing failed; nested
exception is java.util.ConcurrentModificationException] with root cause
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at mine.Controllers.InvoiceContorller.editItemToInvoice(InvoiceContorller.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
Run Code Online (Sandbox Code Playgroud)
for(InvoiceEntities itemnam:show)
Run Code Online (Sandbox Code Playgroud)
在场景后面创建一个 的迭代器show,然后在 for 循环中创建另一个迭代器:
Iterator<InvoiceEntities> iter = show.iterator();
Run Code Online (Sandbox Code Playgroud)
并使用第二个迭代器show通过调用进行修改iter.remove();,而第一个迭代器仍在迭代同一个集合。
有关详细信息,请参阅ConcurrentModificationException文档中以“例如”开头的段落。
| 归档时间: |
|
| 查看次数: |
12922 次 |
| 最近记录: |