ArrayList中的Iterator及内部类源码

ArrayList中的Iterator及内部类源码

    • 源码

源码

// An highlighted block
package java.util;

import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import jdk.internal.access.SharedSecrets;
import jdk.internal.util.ArraysSupport;

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
    ...
	public Iterator<E> iterator() {
	        return new Itr();
	    }
	
	    /**
	     * An optimized version of AbstractList.Itr
	     */
	    private class Itr implements Iterator<E> {
	        int cursor;       // index of next element to return
	        int lastRet = -1; // index of last element returned; -1 if no such
	        int expectedModCount = modCount;
	
	        // prevent creating a synthetic constructor
	        Itr() {}
	
	        public boolean hasNext() {
	            return cursor != size;
	        }
	
	        @SuppressWarnings("unchecked")
	        public E next() {
	            checkForComodification();
	            int i = cursor;
	            if (i >= size)
	                throw new NoSuchElementException();
	            Object[] elementData = ArrayList.this.elementData;
	            if (i >= elementData.length)
	                throw new ConcurrentModificationException();
	            cursor = i + 1;
	            return (E) elementData[lastRet = i];
	        }
	
	        public void remove() {
	            if (lastRet < 0)
	                throw new IllegalStateException();
	            checkForComodification();
	
	            try {
	                ArrayList.this.remove(lastRet);
	                cursor = lastRet;
	                lastRet = -1;
	                expectedModCount = modCount;
	            } catch (IndexOutOfBoundsException ex) {
	                throw new ConcurrentModificationException();
	            }
	        }
	
	        @Override
	        public void forEachRemaining(Consumer<? super E> action) {
	            Objects.requireNonNull(action);
	            final int size = ArrayList.this.size;
	            int i = cursor;
	            if (i < size) {
	                final Object[] es = elementData;
	                if (i >= es.length)
	                    throw new ConcurrentModificationException();
	                for (; i < size && modCount == expectedModCount; i++)
	                    action.accept(elementAt(es, i));
	                // update once at end to reduce heap write traffic
	                cursor = i;
	                lastRet = i - 1;
	                checkForComodification();
	            }
	        }
	
	        final void checkForComodification() {
	            if (modCount != expectedModCount)
	                throw new ConcurrentModificationException();
	        }
	    }
	    ...
}	    

你可能感兴趣的:(java)