Java container --- iterator, java container ---
Any container class must have some way to insert elements and retrieve them again. After all, holding things is the most basic work of containers. For List, add0 is one of the methods for inserting elements, while get () is one of the methods for retrieving elements.
If you think from a higher level, you will find a disadvantage: to use a container, you must program the exact type of the container. It seems that there is nothing wrong with this, but consider the following situation: If it was originally coded against the List, but later found that it would be very convenient to apply the same code to the Set, what should I do at this time? Or I want to write general code from the beginning. They only use containers and do not know or care about the container type. How can I apply the Code without rewriting the code generation to different types of containers?
Iterator(Also a design pattern) can be used to achieve this purpose. An iterator is an object that traverses and selects objects in a sequence. The client programmer does not have to know or care about the underlying structure of the sequence. In addition, an iterator is often called a lightweight object: it has a low cost to create it. Therefore, it is often seen that there are some strange limitations on the iterator, such as JavaIteratorOnly one-way movement is allowed.IteratorIt can only be used:
(1) UsageIterator ()The container is required to return an Iterator. Iterator prepares the first element of the returned sequence.
(2) UseNext ()Obtain the next element in the sequence.
(3) UseHasNext ()Check whether there are any elements in the sequence.
(4) useRemove ()Delete the elements recently returned by the selector.
With Iterator, there is no need to worry about the number of elements in the container. It is a matter of concern to hasNext () and next. If you just traverse the List and do not intend to modify the List object, you can seeForeachThe syntax is more concise.
IteratorYou can also removeNext ()This means that the Next () method must be called before the remove () method is called.
The idea of accepting and passing an object container to execute operations on each object is very powerful.
ListlteratorIs a child type of a more powerful Iterator, itIt can only be used for access to various List classes. Although Iterator can only move forwardListlteratorIt can be moved in two directions. It can also generate an index relative to the previous and next elements pointed to by the selector in the list, and can be usedSet ()Method to replace the last element it has accessed. You can callListlterator() Method generates a Listlterator pointing to the beginning of the List. You can also call the listlterator (n) method to create a Listlterator that points to the element where the List index is n at the beginning.
(1) Foreach traversal of set Elements
1 public class ForeachDemo {2 3/** 4 * @ param args 5 */6 public static void main (String [] args) {7 8/* 9 * JDK1.5 features: 10 * enhance the for loop. Function: Used to traverse the Collection or array. 11 * Format: 12 * for (element type variable: Collection container or array) 13*{14 *} 15*16 * What is the difference between a traditional for loop and an enhanced for loop? 17 * enhancement for must have the target to be traversed. This target can only be a Collectionor array. 18*19 */20 21 Collection coll = new ArrayList (); 22 23 coll. add ("abc1"); 24 coll. add ("abc2"); 25 coll. add ("abc3"); 26 27 for (Object obj: coll) {28 System. out. println (obj); 29} 30/* 31 for (Iterator it = coll. iterator (); it. hasNext ();) {32 Object obj = it. next (); 33 System. out. println (obj); 34 35} */36 // for array traversal, if you do not operate its badge, you can use enhanced for. If you want to operate the badge. Use traditional. 37 int [] arr = {23,15, 32, 78}; 38 for (int x: arr) {39 System. out. println ("x =" + x); 40} 41 42 43} 44 45}
(2) ListIterator traverses List elements
1 public class ListIteratorDemo {2 3/** 4 * @ param args 5 */6 public static void main (String [] args) {7 8 List list = new ArrayList (); 9 10 list. add ("abc1"); 11 list. add ("abc2"); 12 list. add ("abc3"); 13 list. add ("abc4"); 14 15 16/* 17 // during traversal, add an element haha 18 for (Iterator it = list. iterator (); it. hasNext ();) {19 Object obj = it. next (); // java. util. concurrentModificationException 20 2 1 // The set object is used in the iteration process to operate the elements at the same time. This leads to iteration uncertainty. This exception is thrown. 22 // solution: In the iteration process, you can use the iterator to execute some operations. 23 24 if (obj. equals ("abc2") {25 list. add ("haha"); 26} 27} */28 29 // use the unique iterator of the list set. ListIterator obtains the iterator object through the listIterator () method of the List set. 30 // ListIterator can be used to add, delete, modify, and query data during iteration. 31 for (ListIterator it = list. listIterator (); it. hasNext ();) {32 Object obj = it. next (); 33 34 if (obj. equals ("abc2") {35 it. add ("haha"); 36} 37 38} 39 40 System. out. println (list); 41} 42 43}
Content from the Chuanzhi podcast Course