日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

Java Iterator接口實現代碼解析

瀏覽:110日期:2022-09-02 10:38:46

Iterator接口

源代碼

package java.util;import java.util.function.Consumer;/** * An iterator over a collection. {@code Iterator} takes the place of * {@link Enumeration} in the Java Collections Framework. Iterators * differ from enumerations in two ways: * * <ul> * <li> Iterators allow the caller to remove elements from the * underlying collection during the iteration with well-defined * semantics. * <li> Method names have been improved. * </ul> * * <p>This interface is a member of the * <a href='http://m.b3g6.com/bcjs/technotes/guides/collections/index.html' rel='external nofollow' > * Java Collections Framework</a>. * * @param <E> the type of elements returned by this iterator * * @author Josh Bloch * @see Collection * @see ListIterator * @see Iterable * @since 1.2 */public interface Iterator<E> { /** * Returns {@code true} if the iteration has more elements. * (In other words, returns {@code true} if {@link #next} would * return an element rather than throwing an exception.) * * @return {@code true} if the iteration has more elements */ boolean hasNext(); /** * Returns the next element in the iteration. * * @return the next element in the iteration * @throws NoSuchElementException if the iteration has no more elements */ E next(); /** * Removes from the underlying collection the last element returned * by this iterator (optional operation). This method can be called * only once per call to {@link #next}. The behavior of an iterator * is unspecified if the underlying collection is modified while the * iteration is in progress in any way other than by calling this * method. * * @implSpec * The default implementation throws an instance of * {@link UnsupportedOperationException} and performs no other action. * * @throws UnsupportedOperationException if the {@code remove} * operation is not supported by this iterator * * @throws IllegalStateException if the {@code next} method has not * yet been called, or the {@code remove} method has already * been called after the last call to the {@code next} * method */ default void remove() { throw new UnsupportedOperationException('remove'); } /** * Performs the given action for each remaining element until all elements * have been processed or the action throws an exception. Actions are * performed in the order of iteration, if that order is specified. * Exceptions thrown by the action are relayed to the caller. * * @implSpec * <p>The default implementation behaves as if: * <pre>{@code * while (hasNext()) * action.accept(next()); * }</pre> * * @param action The action to be performed for each element * @throws NullPointerException if the specified action is null * @since 1.8 */ default void forEachRemaining(Consumer<? super E> action) { Objects.requireNonNull(action); while (hasNext()) action.accept(next()); }}

閱讀筆記

1. Iterator接口與Enumeration接口的關系/Iterator接口在Java集合庫中的作用

Iterator接口是Java集合框架的一部分,被用于替代原有的Enumeration接口。(“Iterator”比“Enumeration”更簡短、表意更清晰、功能更多,具體的信息下面Enumeration接口的注解中說的挺清楚,且Enumeration注解中也建議編程人員改用Iterator接口)

Java類庫中,集合類的基本接口是Collection接口,而Collection接口實現了Iterable接口,Iterable接口中有一個iterator()方法用于獲取Iterator對象。

package java.util;/** * An object that implements the Enumeration interface generates a * series of elements, one at a time. Successive calls to the * <code>nextElement</code> method return successive elements of the * series. * <p> * For example, to print all elements of a <tt>Vector&lt;E&gt;</tt> <i>v</i>: * <pre> * for (Enumeration&lt;E&gt; e = v.elements(); e.hasMoreElements();) * System.out.println(e.nextElement());</pre> * <p> * Methods are provided to enumerate through the elements of a * vector, the keys of a hashtable, and the values in a hashtable. * Enumerations are also used to specify the input streams to a * <code>SequenceInputStream</code>. * <p> * NOTE: The functionality of this interface is duplicated by the Iterator * interface. In addition, Iterator adds an optional remove operation, and * has shorter method names. New implementations should consider using * Iterator in preference to Enumeration. * * @see java.util.Iterator * @see java.io.SequenceInputStream * @see java.util.Enumeration#nextElement() * @see java.util.Hashtable * @see java.util.Hashtable#elements() * @see java.util.Hashtable#keys() * @see java.util.Vector * @see java.util.Vector#elements() * * @author Lee Boynton * @since JDK1.0 */public interface Enumeration<E> { /** * Tests if this enumeration contains more elements. * * @return <code>true</code> if and only if this enumeration object * contains at least one more element to provide; * <code>false</code> otherwise. */ boolean hasMoreElements(); /** * Returns the next element of this enumeration if this enumeration * object has at least one more element to provide. * * @return the next element of this enumeration. * @exception NoSuchElementException if no more elements exist. */ E nextElement();}

2.hasNext()、next()、remove()方法的關系

hasNext()方法:判斷是否還有元素可以進行迭代;

next()方法:迭代元素;

remove()方法:

/*** Remove from the underlying collection the last element returned by this iterator*(optional operation). * 移除當前迭代器上一次從基礎集合中迭代的元素(可選操作)** This method can be called only once per call to next().* 調用remove()方法前必須先調用next()方法,調用完一次remove()方法后想要再次調用remove()方法,* 必須先調用next()方法。** The behavior of an iterator is unspecified if the underlying collection is modifyed while* the iteration is in progress is any way other than by call this method.* 如果在迭代進行過程中修改了基礎集合,則迭代器的行為是不確定的。*/public static void main(String[] args) { Collection<String> stringCollection = new ArrayList<>(); stringCollection.add('Hello'); stringCollection.add('World'); stringCollection.add('!'); Iterator<String> stringIterator = stringCollection.iterator(); stringIterator.next(); stringIterator.remove();//OK }public static void main(String[] args) { ...... stringIterator.next(); stringCollection.add('abc');//基本集合被改變 stringIterator.remove();//ERROR - java.util.ConcurrentModificationException }public static void main(String[] args) { ...... stringIterator.next(); stringCollection.add('abc');//基本集合被改變 stringIterator.next();//ERROR - java.util.ConcurrentModificationException }public static void main(String[] args) { ...... stringIterator.next(); stringCollection.add('abc');//基本集合改變 stringIterator = stringCollection.iterator();//重新獲取迭代器 stringIterator.next();//OK stringIterator.remove();//OK }

三者關系:調用remove()方法前必須先調用next()方法,調用next()方法前最好先調用hasNext()方法。

3.具體實現類

AbstractList類中定義了一個實現了Iterator接口的內部類:

private class Itr implements Iterator<E> { /** * Index of element to be returned by subsequent call to next. */ int cursor = 0; /** * Index of element returned by most recent call to next or * previous. Reset to -1 if this element is deleted by a call * to remove. */ int lastRet = -1; /** * The modCount value that the iterator believes that the backing * List should have. If this expectation is violated, the iterator * has detected concurrent modification. */ int expectedModCount = modCount; public boolean hasNext() { return cursor != size(); } public E next() { checkForComodification(); try { int i = cursor; E next = get(i); lastRet = i;//最近一次調用next()方法返回的元素的下標。 cursor = i + 1;//下一次調用next()方法返回的元素的下標。 return next; } catch (IndexOutOfBoundsException e) { checkForComodification(); throw new NoSuchElementException(); } } public void remove() { if (lastRet < 0) throw new IllegalStateException();//所以,調用remove()前必須先調用next() checkForComodification(); try { AbstractList.this.remove(lastRet); if (lastRet < cursor)cursor--;//因為移除了一個元素 lastRet = -1;//所以,不能連續調用兩次remove()方法 expectedModCount = modCount; } catch (IndexOutOfBoundsException e) { throw new ConcurrentModificationException(); } } final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); }}

看完上面的代碼,我對modCount、expectedModCount變量以及checkForComodification()方法的作用比較好奇,所以嘗試著去搞清楚。

先來看modeCount變量,這個變量被聲明在內部類的外部:

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> { /** * The number of times this list has been <i>structurally modified</i>. * Structural modifications are those that change the size of the * list, or otherwise perturb it in such a fashion that iterations in * progress may yield incorrect results. * 用于表示該列表發生結構性修改的次數。結構性修改是指*更改列表的大小*或*以其他 * 方式干擾列表*,即正在進行的迭代可能會產生錯誤的結果。 * * <p>This field is used by the iterator and list iterator implementation * returned by the {@code iterator} and {@code listIterator} methods. * If the value of this field changes unexpectedly, the iterator (or list * iterator) will throw a {@code ConcurrentModificationException} in * response to the {@code next}, {@code remove}, {@code previous}, * {@code set} or {@code add} operations. This provides * <i>fail-fast</i> behavior, rather than non-deterministic behavior in * the face of concurrent modification during iteration. * 設計者認為,與其因為基本集合被并發修改從而使迭代產生不確定行為,不如盡早給出錯誤。 * * <p><b>Use of this field by subclasses is optional.</b> If a subclass * wishes to provide fail-fast iterators (and list iterators), then it * merely has to increment this field in its {@code add(int, E)} and * {@code remove(int)} methods (and any other methods that it overrides * that result in structural modifications to the list). A single call to * {@code add(int, E)} or {@code remove(int)} must add no more than * one to this field, or the iterators (and list iterators) will throw * bogus {@code ConcurrentModificationExceptions}. If an implementation * does not wish to provide fail-fast iterators, this field may be * ignored. * 是否使用應需求決定。 */ protected transient int modCount = 0;}

看完上面的源碼注解,已經大概能夠知道modCount、expectedModCount以及checkForComodification()的作用了。

假如把基礎集合當作一個銀行賬號,基礎集合中的元素表示存款。那么modCount就相當于銀行為每個賬號做的消費記錄,expectedModCount就相當于是賬號持有人自己做的一份消費記錄,一般銀行和賬號持有人自己做的消費記錄都不會出錯。

final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException();}

一旦銀行那邊的消費記錄和自己手里的那份消費記錄對不上,肯定是賬號被盜用了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
三上亚洲一区二区| 国产伦精品一区二区三区视频| 蜜桃久久久久久| 日韩高清在线不卡| 国产毛片精品久久| 亚洲成人一区在线观看| 综合激情婷婷| 亚洲欧美伊人| 福利在线免费视频| 日本午夜精品久久久久| 日韩天堂在线| 久久精品国产99国产| 蜜臀av在线播放一区二区三区| 国产二区精品| 在线视频观看日韩| 久色成人在线| 国产精品免费精品自在线观看| 国产欧美一区二区三区精品酒店| 婷婷色综合网| 蜜臀久久99精品久久久久宅男 | 激情偷拍久久| 丝袜美腿高跟呻吟高潮一区| 国产日韩欧美中文在线| 色天使综合视频| 最新亚洲国产| 国产成人精选| 欧美一区二区三区久久| 国产综合婷婷| 欧美日韩18| 伊人精品视频| 国产综合婷婷| 麻豆国产欧美一区二区三区| 免费在线视频一区| 欧产日产国产精品视频| 精品三区视频| 国产精品黄网站| 91精品麻豆| 日韩精品一区二区三区免费视频| 欧美日韩国产探花| 国产麻豆精品| 婷婷五月色综合香五月| 中文一区在线| 丝袜美腿亚洲色图| 国产亚洲一区在线| 狠狠爱成人网| 国产免费成人| 亚洲天堂一区二区| 久久久久99| 香蕉精品久久| 亚洲涩涩在线| 免费精品国产的网站免费观看| 日韩理论片av| 国产经典一区| 激情婷婷综合| 热久久国产精品| 丝袜亚洲另类欧美| 日本午夜精品一区二区三区电影 | 综合五月婷婷| 久久午夜精品| 中文一区在线| 欧美精品国产一区| 国产日韩欧美三级| 麻豆国产精品视频| 99热精品久久| 亚洲精选成人| 视频在线不卡免费观看| 国产一级久久| 另类欧美日韩国产在线| 日韩不卡在线| 欧美一区久久| 日韩av一级| 一区二区三区四区日韩| 精品视频在线你懂得| 五月天久久777| 男人的天堂久久精品| 四虎国产精品免费观看| 美女国产一区| 色一区二区三区| 日韩精品视频中文字幕| 日韩久久电影| 国产精品午夜一区二区三区| 久久人人88| 清纯唯美亚洲综合一区| 电影天堂国产精品| 日本免费一区二区视频| 成人在线网站| 国产精品巨作av| 亚洲深夜影院| 妖精视频成人观看www| 欧美午夜三级| 日韩专区一卡二卡| 国产综合亚洲精品一区二| 成人精品高清在线视频| 国产精品亚洲欧美日韩一区在线| 欧美特黄a级高清免费大片a级| 日韩在线网址| 91精品丝袜国产高跟在线| 欧美亚洲日本精品| 在线亚洲自拍| 国产精品欧美一区二区三区不卡| 国产美女一区| 久久国产日韩| 四季av一区二区凹凸精品| 精品久久影院| 国产精品久久观看| 国产精品yjizz视频网| 日韩av中文在线观看| 青草综合视频| 国产麻豆一区| 九九久久婷婷| 亚洲精品自拍| 国产精品网站在线看| 黄色欧美在线| 香蕉国产精品| 先锋影音国产一区| 国产三级精品三级在线观看国产| 一本综合精品| 日本黄色精品| 亚洲精品免费观看| 精品美女视频 | 麻豆成人在线| 樱桃成人精品视频在线播放| 亚洲精品黄色| 精品亚洲成人| 99国产精品视频免费观看一公开 | 蜜臀久久久99精品久久久久久| 日韩av中文字幕一区| 国产美女精品视频免费播放软件| 国产美女视频一区二区| 99精品视频在线| 久久久久久自在自线| 精品一区二区三区视频在线播放| 国产极品久久久久久久久波多结野| 日韩中文字幕无砖| 日韩欧美一区二区三区在线视频| 日韩精品亚洲专区| 日韩视频久久| 中文字幕高清在线播放| 69堂免费精品视频在线播放| 日本а中文在线天堂| 日韩中文一区二区| 亚洲视频www| 五月天久久网站| 国产高清久久| 伊人久久亚洲影院| 欧美久久精品一级c片| 激情综合激情| 亚洲免费播放| 久久xxxx| 日韩精品视频网站| 国产日韩一区二区三免费高清| 国产日韩一区二区三区在线播放 | 99视频精品| 国产v日韩v欧美v| 91精品一区国产高清在线gif | 国内亚洲精品| 欧美激情麻豆| 麻豆成人91精品二区三区| 欧美国产中文高清| 久久久久久色 | 你懂的亚洲视频| 婷婷久久免费视频| 亚洲精品自拍| 国产精品视频一区二区三区| 成人在线视频中文字幕| 四虎成人av| 欧美日韩免费观看一区=区三区| 亚洲精品看片| 欧美私人啪啪vps| 日韩av首页| 亚洲综合中文| 久久精品国内一区二区三区| 中文字幕系列一区| 亚洲a级精品| 精精国产xxxx视频在线野外 | 日本成人在线不卡视频| 美腿丝袜在线亚洲一区| 三级欧美在线一区| 日韩精品诱惑一区?区三区| 国产日产精品_国产精品毛片| 国产亚洲在线| 精品国产美女a久久9999| 日本欧美一区二区| 丝袜诱惑制服诱惑色一区在线观看| 日韩激情啪啪| 国产色噜噜噜91在线精品| 日韩高清不卡一区| 国产欧美另类| 久久精品国产68国产精品亚洲| 一区在线免费| 欧美专区18| 国产成人免费| 亚洲精品伊人| 欧洲精品一区二区三区| 亚洲毛片网站| 午夜久久美女| 午夜久久中文| 美女久久精品| 日韩一区二区三免费高清在线观看 | 天堂网在线观看国产精品|