我读到在迭代Collection时删除元素的正确方法是这样的(使用迭代器):
List<Integer> list = new ArrayList<Integer>();
list.add(12);
list.add(18);
Iterator<Integer> itr = list.iterator();
while(itr.hasNext()) {
itr.remove();
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到了Exception in thread "main" java.lang.IllegalStateException,我不知道为什么.有人能帮我吗?
如果符合条件,我需要从列表中删除一些对象.
但我得到了java.util.ConcurrentModificationException.
这是我的代码:
collegeList.addAll(CollegeManager.findByCollegeID(stateCode, districtCode));
for(College clg:collegeList){
if(!clg.approve()){
collegeList.remove(clg);
}
}
Run Code Online (Sandbox Code Playgroud) private List<RolePermission> permissionList = new ArrayList<RolePermission>();
ListIterator<RolePermission> iterator = permissionList.listIterator();
permissionList.remove(iterator.next().getRolePermissionName().contains("http"));
Run Code Online (Sandbox Code Playgroud)
我想从列表中删除包含术语"http"的那些项目.但是这段代码不起作用.