當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java List listIterator()用法及代碼示例


此方法從列表中的指定位置開始(按適當順序)返回所提到列表中元素的列表迭代器。

用法:

ListIterator listIterator(int index)

參數:此方法僅具有參數,即index –要從列表迭代器(通過調用next)返回的第一個元素的索引。


返回值:此方法從列表中的指定位置開始(按適當順序)返回列表中元素的列表迭代器。

異常:此方法引發異常IndexOutOfBoundsException-如果索引超出範圍(索引size())

下麵的程序顯示了此方法的實現。

示例1:

// Java program to demonstrate 
// listIterator() method 
// for List interface 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) throws Exception 
    { 
        try { 
  
            // Creating object of List<Integer> 
            List<Integer> arrlist = new ArrayList<>(); 
  
            // adding element to arrlist 
            arrlist.add(1); 
            arrlist.add(3); 
            arrlist.add(6); 
            arrlist.add(9); 
  
            // print arrlist 
            System.out.println("ArrayList: "
                               + arrlist); 
  
            // Creating object of ListIterator<String> 
            // using listIterator() method 
            ListIterator<Integer> 
                iterator = arrlist.listIterator(); 
  
            // Printing the iterated value 
            System.out.println("\nUsing ListIterator:\n"); 
            while (iterator.hasNext()) { 
                System.out.println("Value is : "
                                   + iterator.next()); 
            } 
        } 
  
        catch (NullPointerException e) { 
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
ArrayList: [1, 3, 6, 9]

Using ListIterator:

Value is : 1
Value is : 3
Value is : 6
Value is : 9

示例2:以下代碼顯示了使用Linkedlist實現list.subList()的代碼。

// Java program to demonstrate 
// listIterator() method 
// for List interface 
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) throws Exception 
    { 
        try { 
  
            // Creating object of List<Integer> 
            List<String> arrlist = new ArrayList<String>(); 
  
            // adding element to arrlist 
            arrlist.add("A"); 
            arrlist.add("B"); 
            arrlist.add("C"); 
            arrlist.add("D"); 
  
            // print arrlist 
            System.out.println("ArrayList: "
                               + arrlist); 
  
            // Creating object of ListIterator<String> 
            // using listIterator() method 
            ListIterator<String> 
                iterator = arrlist.listIterator(); 
  
            // Printing the iterated value 
            System.out.println("\nUsing ListIterator:\n"); 
            while (iterator.hasNext()) { 
                System.out.println("Value is : "
                                   + iterator.next()); 
            } 
        } 
  
        catch (NullPointerException e) { 
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
ArrayList: [A, B, C, D]

Using ListIterator:

Value is : A
Value is : B
Value is : C
Value is : D

參考:
Oracle Docs



相關用法


注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 List listIterator() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。