当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。