<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Java `ListIterator`接口 > 原文: [https://www.programiz.com/java-programming/listiterator](https://www.programiz.com/java-programming/listiterator) #### 在本教程中,我們將通過一個示例來學習 Java `ListIterator`接口。 Java 集合框架的`ListIterator`接口提供了訪問列表元素的功能。 它是雙向的。 這意味著它允許我們在兩個方向上迭代列表的元素。 它擴展了`Iterator`接口。 ![The ListIterator interface extends the Java Iterator interface.](https://img.kancloud.cn/35/d4/35d4fe32e74d4762c94601929991c1dc_376x388.png) `List`接口提供了`listIterator()`方法,該方法返回`ListIterator`接口的實例。 * * * ## `ListIterator`的方法 `ListIterator`接口提供了可用于對列表元素執行各種操作的方法。 * `hasNext()` - 如果列表中存在一個元素,則返回`true` * `next()` - 返回列表的下一個元素 * `nextIndex()`返回`next()`方法將返回的元素的索引 * `previous()` - 返回列表的前一個元素 * `previousIndex()` - 返回`previous()`方法將返回的元素的索引 * `remove()` - 刪除`next()`或`previous()`返回的元素 * `set()` - 用指定的元素替換`next()`或`previous()`返回的元素 * * * ### 示例 1:`ListIterator`的實現 在下面的示例中,我們在`ArrayList`中實現了`ListIterator`接口的`next()`,`nextIndex()`和`hasNext()`方法。 ```java import java.util.ArrayList; import java.util.ListIterator; class Main { public static void main(String[] args) { // Creating an ArrayList ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(3); numbers.add(2); System.out.println("ArrayList: " + numbers); // Creating an instance of ListIterator ListIterator<Integer> iterate = numbers.listIterator(); // Using the next() method int number1 = iterate.next(); System.out.println("Next Element: " + number1); // Using the nextIndex() int index1 = iterate.nextIndex(); System.out.println("Position of Next Element: " + index1); // Using the hasNext() method System.out.println("Is there any next element? " + iterate.hasNext()); } } ``` **輸出** ```java ArrayList: [1, 3, 2] Next Element: 1 Position of Next Element: 1 Is there any next element? true ``` * * * ### 示例 2:`ListIterator`的實現 在下面的示例中,我們在`ArrayList`中實現了`ListIterator`接口的`previous()`和`previousIndex()`方法。 ```java import java.util.ArrayList; import java.util.ListIterator; class Main { public static void main(String[] args) { // Creating an ArrayList ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(3); numbers.add(2); System.out.println("ArrayList: " + numbers); // Creating an instance of ListIterator ListIterator<Integer> iterate = numbers.listIterator(); iterate.next(); iterate.next(); // Using the previous() method int number1 = iterate.previous(); System.out.println("Previous Element: " + number1); // Using the previousIndex() int index1 = iterate.previousIndex(); System.out.println("Position of the Previous element: " + index1); } } ``` **輸出**: ```java ArrayList: [1, 3, 2] Previous Element: 3 Position of the Previous Element: 0 ``` 在上面的示例中,最初,`Iterator`的實例在 1 之前。由于在 1 之前沒有元素,因此調用`previous()`方法將引發異常。 然后,我們使用了`next()`方法兩次。 現在`Iterator`實例將在 3 到 2 之間。 因此,`previous()`方法返回 3。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看