How can I fix this error java.util.ConcurrentModif

2020-02-01 04:59发布

I get an error on the following line. I'm doing the process of adding to the jsonarray. Please help me.

jsonArr=new JSONArray();
if(req.getSession().getAttribute("userses")!=null){
    String name=(req.getParameter("name")==null?"":to_EnglishName(req.getParameter("name").toUpperCase()));
    if(!name.equals("")){
        for(Book c:GlobalObjects.bookList){
            if(c.getBookName().startsWith(name)){
                    jsonObjec=new JSONObject();
                    jsonObjec.put("label",c.getBookName());
                    jsonObjec.put("value", c.getId());
                    jsonArr.add(jsonObjec);//java.util.ConcurrentModificationException
            }
        }
    }
}
jsonArr.write(res.getWriter());

7条回答
爷的心禁止访问
2楼-- · 2020-02-01 05:47

Are you accessing jsonArr from another thread? i.e. Iterating over jsonArr when you are modifying it at the same time.

ConcurrentModificationException is thrown when you modify a collection at the same time while iterating it.

查看更多
登录 后发表回答