- /**
- * 使用Iterator的方式也可以顺利删除和遍历
- */
- public void iteratorRemove() {
- List<Student> students = this.getStudents();
- System.out.println(students);
- Iterator<Student> stuIter = students.iterator();
- while (stuIter.hasNext()) {
- Student student = stuIter.next();
- if (student.getId() % 2 == 0)
- stuIter.remove();//这里要使用Iterator的remove方法移除当前对象,如果使用List的remove方法,则同样会出现ConcurrentModificationException
- }
- System.out.println(students);
- }
正确遍历删除List中的元素
猜你喜欢
转载自blog.csdn.net/a695929533/article/details/51900252
今日推荐
周排行