聶永的博客

          記錄工作/學習的點點滴滴。

          循環刪除List數組容器里面元素

          嗯,大部分都會遇到這個問題,定義一個List<T>數組,然后需要循環一下,刪除其中一個元素,但會遇到類似問題:
          java.util.ConcurrentModificationException
          怎么辦,實用主義者會把List數組轉換成Iterator進行迭代刪除,一點問題都沒有:
           public static void main(String[] args) {
          List<Student> students = pareStudents();

          System.out.println("size : " + students.size());
          System.out.println(students);

          System.out.println("delete Student with name xiaoi");

          Iterator<Student>stuIter = students.iterator();

          while(stuIter.hasNext()){
          Student stu = stuIter.next();
          if(stu.getName().equals("xiaoi")){
          stuIter.remove();
          }
          }

          System.out.println("size : " + students.size());
          System.out.println(students);
          }

          Iterator進行循環操作,然后刪除,是很安全的??梢越鉀Q一些問題。但不要再使用Iterator的時候使用students.remove(stu),一樣會給出 java.util.ConcurrentModificationException 錯誤。


          另一個方法就是重新構建一個List,把原先的students 數組作為參數傳遞進去即可,雖然生成了一個全新的數組,但都是Student對象的引用,多個引用的地址數組而已。


           public static void main(String[] args) {
          List<Student> students = pareStudents();

          System.out.println("size : " + students.size());
          System.out.println(students);

          System.out.println("delete Student with name xiaoi");
          for(Student s : new ArrayList<Student>(students)){
          if(s.getName().equals("xiaoi")){
          students.remove(s);
          }
          }

          System.out.println("size : " + students.size());
          System.out.println(students);
          }


          需要那一種方式,那就自便了,個人傾向于使用Iterator。
          完整測試代碼如下:

          public class DeleteList {
          private static class Student{
          private String name;
          private int age;

          public Student(String name, int age) {
          this.name = name;
          this.age = age;
          }

          public int getAge() {
          return age;
          }

          public void setAge(int age) {
          this.age = age;
          }

          public String getName() {
          return name;
          }
          public void setName(String name) {
          this.name = name;
          }

          public int hashCode() {
          return name.hashCode();
          }

          public boolean equals(Object obj) {
          if(obj == null || !(obj instanceof Student)){
          return false;
          }

          Student objStu = (Student)obj;
          return objStu.getName().equals(this.getName()) && objStu.getAge() == this.getAge();
          }

          public String toString(){
          return name + " : " + age;
          }
          }

          private static List<Student> pareStudents(){
          ArrayList<Student> students = new ArrayList<Student>();

          students.add(new Student("xiaoi", 18));
          students.add(new Student("xiaoxin", 19));
          students.add(new Student("john", 20));
          students.add(new Student("xiaomei", 23));

          return students;
          }

          public static void main(String[] args) {
          List<Student> students = pareStudents();

          System.out.println("size : " + students.size());
          System.out.println(students);

          System.out.println("delete Student with name xiaoi");
          for(Student s : new ArrayList<Student>(students)){
          if(s.getName().equals("xiaoi")){
          students.remove(s);
          }
          }

          System.out.println("size : " + students.size());
          System.out.println(students);
          }

          public static void main2(String[] args) {
          List<Student> students = pareStudents();

          System.out.println("size : " + students.size());
          System.out.println(students);

          System.out.println("delete Student with name xiaoi");

          Iterator<Student>stuIter = students.iterator();

          while(stuIter.hasNext()){
          Student stu = stuIter.next();
          if(stu.getName().equals("xiaoi")){
          stuIter.remove();
          }
          }

          System.out.println("size : " + students.size());
          System.out.println(students);
          }
          }

          Iterator 是工作在一個獨立的線程中,并且擁有一個 mutex 鎖。 Iterator 被創建之后會建立一個指向原來對象的單鏈索引表,當原來的對象數量發生變化時,這個索引表的內容不會同步改變,所以當索引指針往后移動的時候就找不到要迭 代的對象,所以按照 fail-fast 原則 Iterator 會馬上拋出 java.util.ConcurrentModificationException 異常。

          所以 Iterator 在工作的時候是不允許被迭代的對象被改變的。但你可以使用 Iterator 本身的方法 remove() 來刪除對象, Iterator.remove() 方法會在刪除當前迭代對象的同時維護索引的一致性。

          refer url : http://www.cnblogs.com/iloveu/archive/2009/06/23/1509385.html

          posted on 2010-10-09 10:36 nieyong 閱讀(4532) 評論(0)  編輯  收藏 所屬分類: Java

          公告

          所有文章皆為原創,若轉載請標明出處,謝謝~

          新浪微博,歡迎關注:

          導航

          <2010年10月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          統計

          常用鏈接

          留言簿(58)

          隨筆分類(130)

          隨筆檔案(151)

          個人收藏

          最新隨筆

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 锦州市| 九龙坡区| 静宁县| 来安县| 柯坪县| 阿拉尔市| 通山县| 章丘市| 道真| 彭阳县| 白沙| 德令哈市| 育儿| 尼勒克县| 金溪县| 宝清县| 和政县| 平山县| 榆林市| 治县。| 东乌珠穆沁旗| 巴南区| 和顺县| 疏附县| 商河县| 澜沧| 鹿邑县| 乌拉特后旗| 澳门| 吉林市| 松原市| 牟定县| 大关县| 辽中县| 沂南县| 台南市| 隆尧县| 社会| 罗源县| 镇江市| 石家庄市|