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


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


  • CopyOnWriteArrayList的lastIndexOf(Object o)方法返回列表中傳遞的元素的最後一次出現。如果列表中不存在該元素,則返回-1。

    用法:

    public int lastIndexOf(Object o)

    參數:該函數接受一個參數o,其最後一次出現將被返回。

    返回值:該函數返回該元素的最後一次出現。如果列表中不存在該元素,則返回-1。


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

    示例1:

    // Java Program to illustrate the CopyOnWriteArrayList 
    // lastIndexOf() 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); 
      
            // last occurrence of 67 
            System.out.println("lastIndexOf value: " 
                             + ArrLis.lastIndexOf(67)); 
        } 
    }
    輸出:
    CopyOnWriteArrayList: [32, 67, 67, 100]
    lastIndexOf value: 2
    

    示例2:

    // Java Program to illustrate the CopyOnWriteArrayList 
    // lastIndexOf() 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); 
      
            // 200 is not present 
            System.out.println("lastIndexOf value: " 
                              + ArrLis.lastIndexOf(200)); 
        } 
    }
    輸出:
    CopyOnWriteArrayList: [32, 67, 67, 100]
    lastIndexOf value: -1
    
  • CopyOnWriteArrayList的lastIndexOf(E e,int index)方法返回位置索引之後在列表中傳遞的元素的最後一次出現。如果列表中不存在該元素,則返回-1。

    用法:

    public int lastIndexOf(E e, int index)

    參數:該函數接受以下兩個參數:

    • index:指定要從中搜索事件的索引。
    • e:指定要返回位置索引中最後一次出現的元素。

    返回值:該函數返回位置索引之後元素的最後一次出現。如果列表中不存在該元素,則返回-1。

    異常:如果指定的索引為負,該函數將引發IndexOutOfBoundsException。

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


    示例1:

    // Java Program to illustrate the CopyOnWriteArrayList 
    // lastIndexOf() 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(67); 
      
            // print CopyOnWriteArrayList 
            System.out.println("CopyOnWriteArrayList: " 
                                                 + ArrLis); 
      
            // last occurrence of 67 from 2nd index 
            System.out.println("lastIndexOf value: " 
                              + ArrLis.lastIndexOf(67, 2)); 
        } 
    }
    輸出:
    CopyOnWriteArrayList: [32, 67, 67, 67]
    lastIndexOf value: 2
    

    示例2:

    // Java Program to illustrate the CopyOnWriteArrayList 
    // lastIndexOf() 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); 
      
            // -1 is out of range, hence exception 
            System.out.println("lastIndexOf value: " 
                           + ArrLis.lastIndexOf(-1, 200)); 
        } 
    }

    輸出:

    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.concurrent.CopyOnWriteArrayList.lastIndexOf(CopyOnWriteArrayList.java:198)
    at java.util.concurrent.CopyOnWriteArrayList.lastIndexOf(CopyOnWriteArrayList.java:263)
    at GFG.main(GFG.java:24)

參考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArrayList.html#lastIndexOf-E-int-



相關用法


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