Java study note 28 (set framework 2: Iterator iterator), study note Iterator
Iterator interface, translated into Chinese as an Iterator
The concept of iterator is interpreted as follows:
There are many collections in java. There are various internal storage methods and different retrieval methods. Is there a common way to obtain them?
This method is similar to for loop traversal,
General method of getting an element: Determine whether this element exists in the set before retrieving the element. If yes, retrieve the element. Continue to judge whether the element exists. If yes, continue to retrieve the element until all is retrieved. This kind of retrieval is iterative.
Implementation of the iterator:
(You can also change the ArrayList set to another set)
Package demo; import java. util. arrayList; import java. util. collection; import java. util. iterator; public class IteratorDemo {public static void main (String [] args) {Collection <String> c1 = new ArrayList <String> (); c1.add ("abc1 "); c1.add ("abc2"); c1.add ("abc3"); c1.add ("abc4"); Iterator <String> it1 = c1.iterator (); boolean b1 = it1.hasNext (); // whether there are elements that can be taken out of System. out. println (b1); // true // obtain the element while (it1.hasNext () {String s = it1.next (); System. out. println (s );}}}
Transformation in collection iteration:
We recommend that you add a type, but do not add it. You can also execute:
Package demo; import java. util. arrayList; import java. util. collection; import java. util. iterator; public class CollectionDemo {public static void main (String [] args) {// set can store any type of objects // set, without specifying the data type, you can also store Collection c1 = new ArrayList (); c1.add ("abc"); c1.add ("def"); Iterator it1 = c1.iterator (); while (it1.hasNext ()) {// it. next gets the Object type and forcibly converts String s1 = (String) it1.next (); System. out. println (s1.length ());}}}
Using the principle of the iterator, an enhanced for loop occurs after JDK1.5:
Example of enhanced for loop traversal array:
Package demo; // enhance the for loop with a fixed format. // advantages: less code, convenient traversal // disadvantages: No index, the public class ForEachDemo {public static void main (String [] args) {function1 (); function2 ();} public static void function1 () in the container cannot be operated () {int [] arr = {3, 2, 5, 4, 8, 9, 6}; // traverses the output for (int I: arr) {System. out. println (I) ;}} public static void function2 () {// when traversing, you can call the method String [] str1 = {"abc", "def ", "java"}; for (String s: str1) {System. out. println (s); System. out. println (s. length ());}}}
Example of an enhanced for loop traversal set:
Package demo; import java. util. arrayList; // enhances the for loop. The regular format is public class ForEachDemo {public static void main (String [] args) {function ();} public static void function () {ArrayList <String> array1 = new ArrayList <String> (); array1.add ("abcd"); array1.add ("java"); for (String s: array1) {System. out. println (s );}}}