List delete elements in the collection you come across such a trap it?

  List of deleting three ways:

 (1) Normal traversal:

 1 @Test
 2     public void testList(){
 3         ArrayList<String> list = new ArrayList<>();
 4         list.add("one");
 5         list.add("two");
 6         list.add("three");
 7         //1. 普通遍历删除 list 中元素
 8         for (int i = 0; i < list.size(); i++) {
 9             if (list.get(i).equals("one")){
10                 list.remove(i);
11             }
12             System.out.println (List.get (I));
 13 is          }
 14          // Result: Normal traversal: successfully deleted 
15      }

  (2) enhanced for loop:

 1 @Test
 2     public void testList(){
 3         ArrayList<String> list = new ArrayList<>();
 4         list.add("one");
 5         list.add("two");
 6         list.add("three");
 7         //2. 增强for循环 删除list中元素
 8         for (String str : list){
 9             if (str.equals("one")){
10                 list.remove("one");
11             }
12             System.out.println(str);
13         }
14         //result: thrown -> a ConcurrentModificationException 
15      }

  (3) iterates over:

 1 @Test
 2     public void testList(){
 3         ArrayList<String> list = new ArrayList<>();
 4         list.add("one");
 5         list.add("two");
 6         list.add("three");
 7         //3. 迭代器遍历 删除 list中元素
 8         Iterator<String> listIter = list.iterator();
 9         while (listIter.hasNext()){
10             String next = listIter.next();
11             if ("one".equals(next)){
12                 listIter.remove ();
 13 is              }
 14              System.out.println (Next);
 15          }
 16          // Result: iterates over successfully deleted 
17          }
 18      }

to sum up:

  (1) Normal traversal: In the code, after deleting elements, the need to index minus one. This is because after each element removal, ArrayList elements will later turn moved up a position (that is, copy), so, the next need to access the index or the current index, it must have to put all minus one elements traversed; <suited for cycling delete a specific element, not suitable for circulation to delete a specific element>

  (2) Enhanced for loop: can not be deleted, thrown a ConcurrentModificationException < List set, in use, object changes can not occur, it will throw an exception >;

  (3) iterates over: Normal deleted.  

  Iterator can delete the normal reasons:

    When the iterator delete elements, is to use the next () Gets the element, and then remove () remove elements, but if between next () and remove (), a set of institutional changes occur (add, delete) will throw IllegalStateException because read next (), the position of the iterator already point to the next, the position is no longer pointing to the current element location.

There has been a collection of structural change (increase or delete) an exception occurs IllegalStateException.

  

Guess you like

Origin www.cnblogs.com/l3306/p/11491872.html