Java Road from entry to advanced of (XXII)

In the previous article we introduced about some of the commonly used methods in Java Collections Framework in the Collection of this chapter we look at the Java Collections Framework iterators Collection's Iterator.

When we created a collection, how to take it from the elements in the collection? Java provides us with an iterator Iterator to help us achieve, as follows:

. 1  Import of java.util.ArrayList;
 2  Import java.util.Collection;
 . 3  Import the java.util.Iterator;
 . 4  
. 5  / ** 
. 6  * through the collection
 . 7  * Collection provides a unified manner through the collection of elements: iterative mode
 8  * iterator iterator () Gets the iterator is used to traverse the current collection
 . 9  * the java.util.Iterator is an interface, a related method for traversing a predetermined set of elements,
 10  * provides a corresponding set of different implementation class
 11  * No need to remember the names of those classes, as only they can Iterator
 12  * <the p->
 13  * loop through the collection follow: Q, take steps to delete, and which to remove is not necessary to operate
 14   * / 
15  public  class Main {
 16      public  static void main (String [] args) {
 . 17          Collection Collection = new new the ArrayList ();
 18 is          Collection.add ( "One" );
 . 19          Collection.add ( "TWO" );
 20 is          Collection.add ( "Three" );
 21 is          Collection .add ( "Four" );
 22 is          // Get current iterators for traversing a set of 
23 is          the iterator iterator = collection.iterator ();
 24          / ** 
25           * Boolean the hasNext () procedure Q
 26           * the determination method is set whether there are elements may be removed
 27           *
 28           * E Next () procedure taken
29           * Get the next element in the collection
 30           * * / 
31 is          the while (iterator.hasNext ()) {
 32              String = String (String) Iterator.next ();
 33 is              System.out.println (String); // One Three TWO Four 
34 is          }
 35      }
 36 }

As can be seen from the above code, we can be output by each element iterator Iterator, that if we want to remove one element of it, since we can traverse out each element, then can we be judged by equals, if it exists, delete it by remove method before speaking, as follows:

. 1  Import of java.util.ArrayList;
 2  Import java.util.Collection;
 . 3  Import the java.util.Iterator;
 . 4  
. 5  public  class the Main {
 . 6      public  static  void main (String [] args) {
 . 7          Collection Collection = new new the ArrayList () ;
 . 8          Collection.add ( "One" );
 . 9          Collection.add ( "TWO" );
 10          Collection.add ( "Three" );
 . 11          Collection.add ( "Four" );
 12 is          // Get current collection for traversing iterator
13 is          the Iterator Iterator = collection.iterator ();
 14          the while (iterator.hasNext ()) {
 15              String = String (String) Iterator.next ();
 16              IF ( "One" .equals (String)) {
 . 17                  / ** 
18                   * when using an iterator loop through the collection, do not use a set of CRUD
 19                   * otherwise it will throw an exception
 20                   * * / 
21                  // Collection.remove (String); // compiler error Exception in thread "main" java .util.ConcurrentModificationException 
22 is                  the Iterator.remove (); //Can directly call comes Iterator remove method is equivalent to a puncturing a 
23 is              }
 24          }
 25          System.out.println (Collection); // [TWO, Three, Four] 
26 is      }
 27 }

We are talking about the array when the array is too traversal operation, and for each method used before, this also applies in the collection, as follows:

. 1  Import of java.util.ArrayList;
 2  Import java.util.Collection;
 . 3  
. 4  / ** 
. 5  * after JDK5.0 introduced a new feature
 6  * Enhanced for loop, also called a new cycle, for each
 . 7  * <P >
 8  * cycle a new cycle can not replace conventional,
 9  * action is used to traverse only collection or array of
 10   * / 
. 11  
12 is  public  class the main {
 13 is      public  static  void main (String [] args) {
 14          String [] array = { "One", "TWO", "Three", "Four" };
 15          for ( int I = 0; I <be array.length; I ++) {
16             System.out.println(array[i]); // one two three four
17         }
18         for (String string : array) {
19             System.out.println(string); // one two three four
20         }
21 
22         Collection collection = new ArrayList();
23         collection.add("one");
24         collection.add("two");
25         collection.add("three");
26         collection.add("four");
27         for (Object object : collection) {
28             String string = (String) object;
29             System.out.println(string); // one two three four
30         }
31     }
32 }

In the above code, we can see that we can be output through the collection form for each collection, then can you delete it by collection.remove (), as follows:

 1 import java.util.ArrayList;
 2 import java.util.Collection;
 3 
 4 public class Main {
 5     public static void main(String[] args) {
 6         Collection collection = new ArrayList();
 7         collection.add("one");
 8         collection.add("two");
 9         collection.add("three");
10         collection.add("four");
11         for (Object object : collection) {
12             String = String (String) Object;
 13 is              IF ( "One" .equals (String)) {
 14                  / ** 
15                   * new cycle is not a new syntax,
 16                   * new cycle is recognized by the compiler, the virtual machine not recognized
 17                   * when using the new loop through, the compiler will read iterator but traverse
 18                   * Therefore, when using the new loop through the set, the method can not set CRUD
 . 19                   * * / 
20 is                  Collection.remove (String); // compile error in the Thread Exception "main" java.util.ConcurrentModificationException 
21              }
 22          }
 23      }
 24- }

The new cycle will not achieve what we want CRUD, we can simply be understood as low old wine in new bottles.

Guess you like

Origin www.cnblogs.com/weijiutao/p/12054033.html