Java 中的 ListIterator


java.util.LinkedList.listIterator(int index) 方法返回此列表中元素(按正確順序)的一個列表迭代器,從列表中的指定位置開始。

宣告

以下是 java.util.LinkedList.listIterator() 方法的宣告

public ListIterator<E> listIterator(int index)

引數

index − 從列表迭代器返回的第一個元素的索引

返回值

此方法返回此列表中元素的一個列表迭代器(按正確順序),從列表中的指定位置開始

異常

IndexOutOfBoundsException − 如果索引超出範圍

示例

以下示例顯示了 java.util.LinkedList.listIterator() 方法的使用。

示例

線上演示

package com.tutorialspoint;

import java.util.*;

public class LinkedListDemo {
   public static void main(String[] args) {

      // create a LinkedList
      LinkedList list = new LinkedList();

      // add some elements
      list.add("Hello");
      list.add(2);
      list.add("Chocolate");
      list.add("10");

      // print the list
      System.out.println("LinkedList:" + list);

      // set Iterator at specified index
      Iterator x = list.listIterator(1);

      // print list with the iterator
      while (x.hasNext()) {
         System.out.println(x.next());
      }
   }
}

讓我們編譯並執行上面的程式,將產生以下結果 −

LinkedList:[Hello, 2, Chocolate, 10]
2
Chocolate
10

更新日期:23-Jun-2020

327 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始
廣告