Java source code parsing iterators

. 1   Private  class Itr the implements the Iterator <E> {
 2          int Cursor;        // index next invoked method returns the element 
. 3          int lastRet = -1; // recent call to the next method returns the index of the element (if there is no next method invocation through this value is equal to -1) 
. 4          int expectedModCount = ModCount;
 . 5  
. 6          Itr () {}
 . 7  
. 8          / * 
. 9          * is determined whether there are elements of the cursor to the position, i.e., whether the next iteration of the iterator element also acquired.
10          * / 
. 11          public  Boolean the hasNext () {
 12 is              return Cursor =! Size;
 13 is          }
14  
15          / * 
16          * Return cursor to the position of the element, and then move the cursor.
. 17          * / 
18 is          @SuppressWarnings ( "an unchecked" )
 . 19          public E next () {
 20 is              checkForComodification ();
 21 is              // . 1, it is necessary to call the method before the call to the next hasNext determination method - avoids exception 
22 is              int I = Cursor;
 23 is              IF (I> = size)
 24                  the throw  new new NoSuchElementException ();
 25              . Object [] = the ArrayList elementData of the this .elementData;
 26 is              IF (I> = elementData.length)
27                  the throw  new new a ConcurrentModificationException ();
 28              // 2, to move the cursor, returns a value after 
29              Cursor = I +. 1 ;
 30              // . 3, the recording position of this variable lastRest return element set 
31 is              return (E) elementData of [lastRet = I];
 32          }
 33 is  
34 is          public  void Remove () {
 35              IF (lastRet <0 )
 36                  the throw  new new IllegalStateException ();
 37 [              checkForComodification ();
 38 is  
39              the try {
 40                  //2, it can be seen from here, is the Remove method of removing a return call to the next element method. 
41 is                  the ArrayList. The this .remove (lastRet);
 42 is                  Cursor = lastRet;
 43 is                  // . 3, it can be seen from here, remove method and next methods are closely related.
44                  // Only call the next method to invoke the remove method, remove method calls can not be contacted,
 45                  // must be called before the first call to the next method remove method under. 
46 is                  lastRet = -1 ;
 47                  expectedModCount = ModCount;
 48              } the catch (an IndexOutOfBoundsException EX) {
 49                  the throw  new new a ConcurrentModificationException ();
 50             }
51         }
52 
53         @Override
54         @SuppressWarnings("unchecked")
55         public void forEachRemaining(Consumer<? super E> consumer) {
56             Objects.requireNonNull(consumer);
57             final int size = ArrayList.this.size;
58             int i = cursor;
59             if (i >= size) {
60                 return;
61             }
62             final Object[] elementData = ArrayList.this.elementData;
63             if (i >= elementData.length) {
64                 throw new ConcurrentModificationException();
65             }
66             while (i != size && modCount == expectedModCount) {
67                 consumer.accept((E) elementData[i++]);
68             }
69             // update once at end of iteration to reduce heap write traffic
70             cursor = i;
71             lastRet = i - 1;
72             checkForComodification();
73         }
74 
75         final void checkForComodification() {
76             if (modCount != expectedModCount)
77                 throw new ConcurrentModificationException();
78         }
79     }

 

Guess you like

Origin www.cnblogs.com/KenBaiCaiDeMiao/p/12003380.html