最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java  Iterator接口和LIstIterator接口分析

 更新時間:2017年05月28日 08:55:10   作者:非水非云  
這篇文章主要介紹了java Iterator接口和LIstIterator接口分析的相關資料,需要的朋友可以參考下

java  Iterator接口和LIstIterator接口分析

目錄

1.Iterator接口
2.ListIterator
3.Iterator和ListIterator的區(qū)別 

正文

在繼續(xù)看ArrayList源碼之前,先了解Iterator接口和ListIterator接口,下篇文章詳細講解ArrayList是如何實現(xiàn)它們的。

我們知道,接口只是一種規(guī)范,當繼承接口并實現(xiàn)其中的方法時,要遵循接口對方法的說明。

1.Iterator接口

Iterator接口取代了Java集合框架中的Enumeratrion。Iterators不同于enumerations的地方主要有兩點:

  Iterators允許調用者在迭代過程中從集合里移除元素;

  方法名得到了改善。

Iterator源碼如下:

/**
 * 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:
 * Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
 * Method names have been improved.
 * This interface is a member of the Java Collections Framework.
 * @param <E> the type of elements returned by this iterator*/
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());
  }
}

Iterator接口定義了四個方法以及各個方法的功能,如果有類實現(xiàn)了這個接口,且實現(xiàn)了這些方法,這方法需要實現(xiàn)定義的功能,遵循這些規(guī)則:

  1).hasNext() 判斷容器是否有下一個元素,有則返回true;

  2).next() 返回容器中的下一個元素;

  3).remove() 移除當前迭代器返回的最后一個元素。這個方法在每次調用next()方法之后只能調用一次;

  4).Java 8 增加forEachRemaining方法,它可以實現(xiàn)對余下的所有元素執(zhí)行指定的操作。

更詳細的說明請閱讀源碼中的注釋。

2.ListIterator

ListIterator在Iterator基礎上提供了add、set、previous等對列表的操作。但是ListIterator跟Iterator一樣,仍是在原列表上進行操作。

ListIterator源碼如下:

/**
 * An iterator for lists that allows the programmer
 * to traverse the list in either direction, modify
 * the list during iteration, and obtain the iterator's
 * current position in the list. A {@code ListIterator}
 * has no current element; its <I>cursor position</I> always
 * lies between the element that would be returned by a call
 * to {@code previous()} and the element that would be
 * returned by a call to {@code next()}.
 * An iterator for a list of length {@code n} has {@code n+1} possible
 * cursor positions, as illustrated by the carets ({@code ^}) below:
 * <PRE>
 *           Element(0)  Element(1)  Element(2)  ... Element(n-1)
 * cursor positions: ^      ^      ^      ^         ^
 * </PRE>
 * Note that the {@link #remove} and {@link #set(Object)} methods are
 * <i>not</i> defined in terms of the cursor position; they are defined to
 * operate on the last element returned by a call to {@link #next} or
 * {@link #previous()}.
 *
 * This interface is a member of the Java Collections Framework.*/
public interface ListIterator<E> extends Iterator<E> {
  // Query Operations

  /**
   * Returns {@code true} if this list iterator has more elements when
   * traversing the list in the forward direction. (In other words,
   * returns {@code true} if {@link #next} would return an element rather
   * than throwing an exception.)
   *
   * @return {@code true} if the list iterator has more elements when
   *     traversing the list in the forward direction
   */
  boolean hasNext();

  /**
   * Returns the next element in the list and advances the cursor position.
   * This method may be called repeatedly to iterate through the list,
   * or intermixed with calls to {@link #previous} to go back and forth.
   * (Note that alternating calls to {@code next} and {@code previous}
   * will return the same element repeatedly.)
   *
   * @return the next element in the list
   * @throws NoSuchElementException if the iteration has no next element
   */
  E next();

  /**
   * Returns {@code true} if this list iterator has more elements when
   * traversing the list in the reverse direction. (In other words,
   * returns {@code true} if {@link #previous} would return an element
   * rather than throwing an exception.)
   *
   * @return {@code true} if the list iterator has more elements when
   *     traversing the list in the reverse direction
   */
  boolean hasPrevious();

  /**
   * Returns the previous element in the list and moves the cursor
   * position backwards. This method may be called repeatedly to
   * iterate through the list backwards, or intermixed with calls to
   * {@link #next} to go back and forth. (Note that alternating calls
   * to {@code next} and {@code previous} will return the same
   * element repeatedly.)
   *
   * @return the previous element in the list
   * @throws NoSuchElementException if the iteration has no previous
   *     element
   */
  E previous();

  /**
   * Returns the index of the element that would be returned by a
   * subsequent call to {@link #next}. (Returns list size if the list
   * iterator is at the end of the list.)
   *
   * @return the index of the element that would be returned by a
   *     subsequent call to {@code next}, or list size if the list
   *     iterator is at the end of the list
   */
  int nextIndex();

  /**
   * Returns the index of the element that would be returned by a
   * subsequent call to {@link #previous}. (Returns -1 if the list
   * iterator is at the beginning of the list.)
   *
   * @return the index of the element that would be returned by a
   *     subsequent call to {@code previous}, or -1 if the list
   *     iterator is at the beginning of the list
   */
  int previousIndex();


  // Modification Operations

  /**
   * Removes from the list the last element that was returned by {@link
   * #next} or {@link #previous} (optional operation). This call can
   * only be made once per call to {@code next} or {@code previous}.
   * It can be made only if {@link #add} has not been
   * called after the last call to {@code next} or {@code previous}.
   *
   * @throws UnsupportedOperationException if the {@code remove}
   *     operation is not supported by this list iterator
   * @throws IllegalStateException if neither {@code next} nor
   *     {@code previous} have been called, or {@code remove} or
   *     {@code add} have been called after the last call to
   *     {@code next} or {@code previous}
   */
  void remove();

  /**
   * Replaces the last element returned by {@link #next} or
   * {@link #previous} with the specified element (optional operation).
   * This call can be made only if neither {@link #remove} nor {@link
   * #add} have been called after the last call to {@code next} or
   * {@code previous}.
   *
   * @param e the element with which to replace the last element returned by
   *     {@code next} or {@code previous}
   * @throws UnsupportedOperationException if the {@code set} operation
   *     is not supported by this list iterator
   * @throws ClassCastException if the class of the specified element
   *     prevents it from being added to this list
   * @throws IllegalArgumentException if some aspect of the specified
   *     element prevents it from being added to this list
   * @throws IllegalStateException if neither {@code next} nor
   *     {@code previous} have been called, or {@code remove} or
   *     {@code add} have been called after the last call to
   *     {@code next} or {@code previous}
   */
  void set(E e);

  /**
   * Inserts the specified element into the list (optional operation).
   * The element is inserted immediately before the element that
   * would be returned by {@link #next}, if any, and after the element
   * that would be returned by {@link #previous}, if any. (If the
   * list contains no elements, the new element becomes the sole element
   * on the list.) The new element is inserted before the implicit
   * cursor: a subsequent call to {@code next} would be unaffected, and a
   * subsequent call to {@code previous} would return the new element.
   * (This call increases by one the value that would be returned by a
   * call to {@code nextIndex} or {@code previousIndex}.)
   *
   * @param e the element to insert
   * @throws UnsupportedOperationException if the {@code add} method is
   *     not supported by this list iterator
   * @throws ClassCastException if the class of the specified element
   *     prevents it from being added to this list
   * @throws IllegalArgumentException if some aspect of this element
   *     prevents it from being added to this list
   */
  void add(E e);
}

ListIterator的功能更加強大,定義的方法有:

  1).hasNext() 向前遍歷時,如果有下一個元素返回真;

  2).next() 返回下一個元素的值,并將指針加1;

  3).hasPrevious() 向相反方向遍歷時,如果還有元素返回真;

  4).previous() 返回上一個元素的值,并將指針前移1;

  5).nextIndex() 返回此時調用next()方法時返回的元素的索引;

  6).previousIndex() 返回此時調用previous()方法時返回的元素的索引;

  7).remove() 移除最近一次調用next()或previous()方法返回的元素(可選);

  8).set(E e) 用元素e將如果此時調用next()或previous()方法返回的元素替換掉;

  9).add(E e) 添加元素到此時調用next()返回的元素之前,或此時調用previous()返回的元素之后。

更詳細的說明請閱讀源碼中的注釋。

3.Iterator和ListIterator的區(qū)別

  Iterator和ListIterator的方法對比如下表:

Iterator

ListIterator

 

hasNext()

hasNext() 覆蓋

next()

next() 覆蓋

remove()

remove() 覆蓋

forEachRemaining(Consumer<? super E> action)

forEachRemaining(Consumer<? super E> action) 繼承
  hasPrevious()  
  previous()  
  nextIndex()  
  previousIndex()  
  set(E e)  
  add(E e)  

二者的不同之處主要有:

  1).Iterator只能單向移動,ListIterator可以雙向移動;

  2).ListIterator可以刪除、替換或添加元素,而Iterator只能刪除元素;

  3).ListIterator可以返回當前(調用next()或previous()返回的)元素的索引,而Iterator不能。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關文章

  • Maven中pom.xml文件報錯的原因解決

    Maven中pom.xml文件報錯的原因解決

    創(chuàng)建Maven項目的時候,如果你選擇的Packaging為war,那么就會報錯,本文主要介紹了Maven中pom.xml文件報錯的原因解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07
  • SpringBoot3集成RocketMq場景分析

    SpringBoot3集成RocketMq場景分析

    RocketMQ因其架構簡單、業(yè)務功能豐富、具備極強可擴展性等特點被廣泛應用,比如金融業(yè)務、互聯(lián)網(wǎng)、大數(shù)據(jù)、物聯(lián)網(wǎng)等領域的業(yè)務場景,這篇文章主要介紹了SpringBoot3集成RocketMq,需要的朋友可以參考下
    2023-08-08
  • SpringBoot使用ApplicationEvent&Listener完成業(yè)務解耦

    SpringBoot使用ApplicationEvent&Listener完成業(yè)務解耦

    這篇文章主要介紹了SpringBoot使用ApplicationEvent&Listener完成業(yè)務解耦示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • 如何使用Spring Cloud Feign日志查看請求響應

    如何使用Spring Cloud Feign日志查看請求響應

    這篇文章主要介紹了如何使用Spring Cloud Feign日志查看請求響應,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • SpringBoot集成Mybatis過程步驟圖解

    SpringBoot集成Mybatis過程步驟圖解

    這篇文章主要介紹了SpringBoot集成Mybatis過程步驟圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • SpringMVC參數(shù)傳遞之基本數(shù)據(jù)類型和復雜對象說明

    SpringMVC參數(shù)傳遞之基本數(shù)據(jù)類型和復雜對象說明

    這篇文章主要介紹了SpringMVC參數(shù)傳遞之基本數(shù)據(jù)類型和復雜對象說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Java代理模式的示例詳解

    Java代理模式的示例詳解

    代理模式(Proxy Parttern)為一個對象提供一個替身,來控制這個對象的訪問,即通過代理對象來訪問目標對象。本文將通過示例詳細講解一下這個模式,需要的可以參考一下
    2022-02-02
  • SpringBoot lombok(注解@Getter @Setter)詳解

    SpringBoot lombok(注解@Getter @Setter)詳解

    通過使用Lombok庫,SpringBoot應用可以自動化生成常用的方法如setter和getter,顯著降低了代碼冗余并提高了開發(fā)效率,Lombok的@Getter和@Setter注解用于自動生成屬性的訪問和修改方法,而@Data注解則提供了一個全面的解決方案
    2024-11-11
  • Spring Boot集成Redis實現(xiàn)緩存機制(從零開始學Spring Boot)

    Spring Boot集成Redis實現(xiàn)緩存機制(從零開始學Spring Boot)

    這篇文章主要介紹了Spring Boot集成Redis實現(xiàn)緩存機制(從零開始學Spring Boot),需要的朋友可以參考下
    2017-04-04
  • Java 本地方法Native Method詳細介紹

    Java 本地方法Native Method詳細介紹

    這篇文章主要介紹了 Java 本地方法Native Method詳細介紹的相關資料,需要的朋友可以參考下
    2017-02-02

最新評論

韩城市| 嘉禾县| 凤翔县| 义马市| 绥阳县| 常宁市| 连平县| 南宁市| 安徽省| 靖江市| 平阳县| 宣武区| 皋兰县| 江口县| 铁力市| 陆良县| 正安县| 文成县| 上思县| 张北县| 年辖:市辖区| 始兴县| 集贤县| 武鸣县| 东光县| 龙岩市| 邻水| 乌审旗| 河南省| 浦北县| 隆昌县| 迁西县| 华容县| 湄潭县| 西畴县| 元朗区| 丰县| 鱼台县| 炎陵县| 盐亭县| 惠东县|