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


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


  • CopyOnWriteArrayList的listIterator()方法以適當的順序返回此列表中元素的listIterator。 listIterator不支持remove,set或add方法。

    用法:

    public ListIterator listIterator()

    參數:該函數不接受任何參數。

    返回值:該函數返回此列表中元素的列表迭代器(按正確順序)


    以下示例程序旨在說明上述函數:

    示例1:

    // Java Program to illustrate the CopyOnWriteArrayList 
    // iterator() method in Java 
    import java.util.concurrent.CopyOnWriteArrayList; 
    import java.util.*; 
      
    public class GFG { 
        public static void main(String[] args) 
        { 
      
            // create object of CopyOnWriteArrayList 
            CopyOnWriteArrayList<Integer> ArrLis 
                = new CopyOnWriteArrayList<Integer>(); 
      
            // Add elements 
            ArrLis.add(32); 
            ArrLis.add(67); 
            ArrLis.add(67); 
            ArrLis.add(100); 
      
            // print CopyOnWriteArrayList 
            System.out.println("CopyOnWriteArrayList: " + ArrLis); 
      
            // Call listIterator() method of 
            Iterator iteratorVals = ArrLis.listIterator(); 
      
            // Print elements of iterator 
            // created from CopyOnWriteArrayList 
            System.out.println("The iterator values"
                               + " of CopyOnWriteArrayList are:"); 
      
            // prints the elements using an iterator 
            while (iteratorVals.hasNext()) { 
                System.out.println(iteratorVals.next()); 
            } 
        } 
    }
    輸出:
    CopyOnWriteArrayList: [32, 67, 67, 100]
    The iterator values of CopyOnWriteArrayList are:
    32
    67
    67
    100
    
  • CopyOnWriteArrayList的listIterator(index)方法從特定索引開始,以適當的順序在此列表中的元素上返回一個listIterator。 listIterator不支持remove,set或add方法。

    用法:

    public ListIterator listIterator(int index)

    參數:該函數接受參數索引,該索引指定要從列表迭代器返回的第一個元素的索引。

    返回值:該函數從指定的索引(以正確的順序)開始,在此列表中的元素上返回列表迭代器。

    異常:如果索引超出範圍,該函數將拋出IndexOutOfBoundsException。

    以下示例程序旨在說明上述函數:

    示例1:

    // Java Program to illustrate the CopyOnWriteArrayList 
    // listIterator() method in Java 
    import java.util.concurrent.CopyOnWriteArrayList; 
    import java.util.*; 
      
    public class GFG { 
        public static void main(String[] args) 
        { 
      
            // create object of CopyOnWriteArrayList 
            CopyOnWriteArrayList<String> ArrLis 
                = new CopyOnWriteArrayList<String>(); 
      
            // Add elements 
            ArrLis.add("gopal"); 
            ArrLis.add("gfg"); 
            ArrLis.add("jgec"); 
            ArrLis.add("sudo"); 
      
            // print CopyOnWriteArrayList 
            System.out.println("CopyOnWriteArrayList: " + ArrLis); 
      
            // Call listIterator() method from 2nd index 
            Iterator iteratorVals = ArrLis.listIterator(2); 
      
            // Print elements of iterator 
            // created from CopyOnWriteArrayList 
            System.out.println("The iterator values"
                               + " of CopyOnWriteArrayList are:"); 
      
            // prints the elements using an iterator 
            while (iteratorVals.hasNext()) { 
                System.out.println(iteratorVals.next()); 
            } 
        } 
    }
    輸出:
    CopyOnWriteArrayList: [gopal, gfg, jgec, sudo]
    The iterator values of CopyOnWriteArrayList are:
    jgec
    sudo
    

    程序2

    // Java Program to illustrate the CopyOnWriteArrayList 
    // listIterator() method in Java, exception 
    import java.util.concurrent.CopyOnWriteArrayList; 
    import java.util.*; 
      
    public class GFG { 
        public static void main(String[] args) 
        { 
      
            // create object of CopyOnWriteArrayList 
            CopyOnWriteArrayList<String> ArrLis 
                = new CopyOnWriteArrayList<String>(); 
      
            // Add elements 
            ArrLis.add("gopal"); 
            ArrLis.add("gfg"); 
            ArrLis.add("jgec"); 
            ArrLis.add("sudo"); 
      
            // print CopyOnWriteArrayList 
            System.out.println("CopyOnWriteArrayList: " + ArrLis); 
      
            // Call listIterator() method from 6th index 
            // hence exception is returned 
            Iterator iteratorVals = ArrLis.listIterator(6); 
      
            // Print elements of iterator 
            // created from CopyOnWriteArrayList 
            System.out.println("The iterator values"
                               + " of CopyOnWriteArrayList are:"); 
      
            // prints the elements using an iterator 
            while (iteratorVals.hasNext()) { 
                System.out.println(iteratorVals.next()); 
            } 
        } 
    }

    輸出:

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 6
        at java.util.concurrent.CopyOnWriteArrayList.listIterator(CopyOnWriteArrayList.java:1107)
        at GFG.main(GFG.java:25)

參考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArrayList.html#listIterator–



相關用法


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