日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如何正确遍历删除List中的元素,你会吗?

發布時間:2025/3/21 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何正确遍历删除List中的元素,你会吗? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

遍歷刪除List中的元素有很多種方法,當運用不當的時候就會產生問題。下面主要看看以下幾種遍歷刪除List中元素的形式:

1.通過增強的for循環刪除符合條件的多個元素

2.通過增強的for循環刪除符合條件的一個元素

3.通過普通的for刪除刪除符合條件的多個元素

4.通過Iterator進行遍歷刪除符合條件的多個元素

?

?

Java代碼??
  • /**?
  • ?*?使用增強的for循環?
  • ?*?在循環過程中從List中刪除元素以后,繼續循環List時會報ConcurrentModificationException?
  • ?*/??
  • public?void?listRemove()?{??
  • ????List<Student>?students?=?this.getStudents();??
  • ????for?(Student?stu?:?students)?{??
  • ????????if?(stu.getId()?==?2)???
  • ????????????students.remove(stu);??
  • ????}??
  • }??
  • ?

    ?

    Java代碼??
  • /**?
  • ?*?像這種使用增強的for循環對List進行遍歷刪除,但刪除之后馬上就跳出的也不會出現異常?
  • ?*/??
  • public?void?listRemoveBreak()?{??
  • ????List<Student>?students?=?this.getStudents();??
  • ????for?(Student?stu?:?students)?{??
  • ????????if?(stu.getId()?==?2)?{??
  • ????????????students.remove(stu);??
  • ????????????break;??
  • ????????}??
  • ????}??
  • }??
  • ?

    ?

    Java代碼??
  • /**?
  • ?*?這種遍歷有可能會遺漏某個元素,因為刪除元素后List的size在?
  • ?*?變化,元素的索引也在變化,比如你循環到第2個元素的時候你把它刪了,?
  • ?*?接下來你去訪問第3個元素,實際上訪問到的是原先的第4個元素。當訪問的元素?
  • ?*?索引超過了當前的List的size后還會出現數組越界的異常,當然這里不會出現這種異常,?
  • ?*?因為這里每遍歷一次都重新拿了一次當前List的size。?
  • ?*/??
  • public?void?listRemove2()?{??
  • ????List<Student>?students?=?this.getStudents();??
  • ????for?(int?i=0;?i<students.size();?i++)?{??
  • ????????if?(students.get(i).getId()%3?==?0)?{??
  • ????????????Student?student?=?students.get(i);??
  • ????????????students.remove(student);??
  • ????????}??
  • ????}??
  • }??
  • ?

    ?

    Java代碼??
  • /**?
  • ?*?使用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);??
  • }??
  • ?

    ?

    附完整代碼如下:

    ?

    Java代碼??
  • import?java.util.ArrayList;??
  • import?java.util.Iterator;??
  • import?java.util.List;??
  • ??
  • public?class?ListRemove?{??
  • ??
  • ????public?static?void?main(String?args[])?{??
  • ????????ListRemove?lr?=?new?ListRemove();??
  • ????????lr.listRemove();??
  • ????????lr.listRemoveBreak();??
  • //??????lr.listRemove2();??
  • //??????lr.iteratorRemove();??
  • ????}??
  • ??
  • ????/**?
  • ?????*?使用增強的for循環?
  • ?????*?在循環過程中從List中刪除元素以后,繼續循環List時會報ConcurrentModificationException?
  • ?????*/??
  • ????public?void?listRemove()?{??
  • ????????List<Student>?students?=?this.getStudents();??
  • ????????for?(Student?stu?:?students)?{??
  • ????????????if?(stu.getId()?==?2)???
  • ????????????????students.remove(stu);??
  • ????????}??
  • ????}??
  • ??????
  • ????/**?
  • ?????*?像這種使用增強的for循環對List進行遍歷刪除,但刪除之后馬上就跳出的也不會出現異常?
  • ?????*/??
  • ????public?void?listRemoveBreak()?{??
  • ????????List<Student>?students?=?this.getStudents();??
  • ????????for?(Student?stu?:?students)?{??
  • ????????????if?(stu.getId()?==?2)?{??
  • ????????????????students.remove(stu);??
  • ????????????????break;??
  • ????????????}??
  • ????????}??
  • ????}??
  • ??????
  • ????/**?
  • ?????*?這種不使用增強的for循環,每次重新獲取list的size遍歷的情況運行時不會報錯,但是可能刪除的結果是錯的。?
  • ?????*/??
  • ????public?void?listRemove2()?{??
  • ????????List<Student>?students?=?this.getStudents();??
  • ????????for?(int?i=0;?i<students.size();?i++)?{??
  • ????????????if?(students.get(i).getId()%2?==?0)??
  • ????????????????students.remove(i);??
  • ????????}??
  • ????}??
  • ??????
  • ????/**?
  • ?????*?使用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();??
  • ????????}??
  • ????????System.out.println(students);??
  • ????}??
  • ??????
  • ????private?List<Student>?getStudents()?{??
  • ????????List<Student>?students?=?new?ArrayList<Student>()?{??
  • ????????????{??
  • ????????????????int?i?=?0;??
  • ????????????????while?(i++?<?10)?{??
  • ????????????????????Student?student?=?new?Student(i,?"201200"?+?i,?"name_"?+?i);??
  • ????????????????????this.add(student);??
  • ????????????????}??
  • ????????????}??
  • ????????};??
  • ????????return?students;??
  • ????}??
  • }??
  • ?

    ?

    Java代碼??
  • public?class?Student?{??
  • ??
  • ????private?int?id;??
  • ????private?String?stuNo;??
  • ????private?String?name;??
  • ??????
  • ????public?Student()?{??
  • ??????????
  • ????}??
  • ??????
  • ????public?Student(int?id,?String?stuNo,?String?name)?{??
  • ????????this.id?=?id;??
  • ????????this.stuNo?=?stuNo;??
  • ????????this.name?=?name;??
  • ????}??
  • ??
  • ????public?int?getId()?{??
  • ????????return?id;??
  • ????}??
  • ??
  • ????public?void?setId(int?id)?{??
  • ????????this.id?=?id;??
  • ????}??
  • ??
  • ????public?String?getStuNo()?{??
  • ????????return?stuNo;??
  • ????}??
  • ??
  • ????public?void?setStuNo(String?stuNo)?{??
  • ????????this.stuNo?=?stuNo;??
  • ????}??
  • ??
  • ????public?String?getName()?{??
  • ????????return?name;??
  • ????}??
  • ??
  • ????public?void?setName(String?name)?{??
  • ????????this.name?=?name;??
  • ????}??
  • ??
  • ????@Override??
  • ????public?String?toString()?{??
  • ????????return?"Student?[id="?+?id?+?",?name="?+?name?+?",?stuNo="?+?stuNo??
  • ????????????????+?"]";??
  • ????}??
  • ??????
  • }

  • from:?http://elim.iteye.com/blog/1523785

    總結

    以上是生活随笔為你收集整理的如何正确遍历删除List中的元素,你会吗?的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。