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


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


listIterator()

java.util.ArrayList類的listIterator()方法用於返回此列表中的元素的列表迭代器(按適當順序)。返回的列表迭代器為fail-fast。

用法:

public ListIterator listIterator()

返回值:此方法返回該列表中的元素的列表迭代器(按適當順序)。


以下示例說明了listIterator()方法。

示例1:

// Java program to demonstrate 
// listIterator() method 
// for String value 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) throws Exception 
    { 
        try { 
  
            // Creating object of ArrayList<Integer> 
            ArrayList<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

listIterator(int index)

此方法用於從列表中的指定位置開始(按適當順序)返回列表中元素的列表迭代器。指定的索引指示首次調用next將返回的第一個元素。最初調用previous將返回指定索引減一的元素。返回的列表迭代器為fail-fast。

用法:

public ListIterator listIterator(int index)

參數:此方法將第一個元素的索引作為要從列表迭代器返回的參數(通過調用next)

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

異常:如果索引超出範圍(索引size()),則此方法將引發IndexOutOfBoundsException。

以下示例說明了listIterator()方法。

示例1:

// Java program to demonstrate 
// listIterator() method 
// for String value 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) throws Exception 
    { 
        try { 
  
            // Creating object of ArrayList<Integer> 
            ArrayList<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); 
  
            // getting iterated value starting from index 2 
            // using listIterator() method 
            ListIterator<String> 
                iterator = arrlist.listIterator(2); 
  
            // Printing the iterated value 
            System.out.println("\nUsing ListIterator"
                               + " from Index 2:\n"); 
            while (iterator.hasNext()) { 
                System.out.println("Value is : "
                                   + iterator.next()); 
            } 
        } 
  
        catch (IndexOutOfBoundsException e) { 
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
ArrayList: [A, B, C, D]

Using ListIterator from Index 2:

Value is : C
Value is : D

示例2:對於IndexOutOfBoundsException

// Java program to demonstrate 
// listIterator() method 
// for IndexOutOfBoundsException 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) throws Exception 
    { 
        try { 
  
            // Creating object of ArrayList<Integer> 
            ArrayList<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); 
            System.out.println("Size of ArrayList: "
                               + arrlist.size()); 
  
            // Get ListIterator from index 7 
            // using listIterator() method 
            System.out.println("Trying to getListIterator"
                               + " from index 7\n"); 
  
            ListIterator<String> 
                iterator = arrlist.listIterator(7); 
        } 
  
        catch (IndexOutOfBoundsException e) { 
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
ArrayList: [A, B, C, D]
Size of ArrayList: 4
Trying to getListIterator from index 7

Exception thrown : java.lang.IndexOutOfBoundsException: Index: 7


相關用法


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