Skip to content
Related Articles

Related Articles

Improve Article

CopyOnWriteArrayList indexOf() method in Java

  • Last Updated : 19 Nov, 2019
  • The indexOf(Object o) method of CopyOnWriteArrayList returns the first occurrence of the element passed in the list. It returns -1 if the element is not present in the list.

    Syntax:

    public int indexOf(Object o)

    Parameters: The function accepts a parameter o whose first occurrence is to be returned.

    Return Value: The function returns the first occurrence of the element. It returns -1 if the element is not present in the list.

    Below programs illustrate the above function:

    Program 1:






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

    Program 2:




    // Java Program to illustrate the CopyOnWriteArrayList
    // indexOf() 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("indexOf value: " 
                             + ArrLis.indexOf(200));
        }
    }
    Output:
    CopyOnWriteArrayList: [32, 67, 67, 100]
    indexOf value: -1
    
  • The indexOf(E e, int index) method of CopyOnWriteArrayList returns the first occurrence of the element passed in the list after position index. It returns -1 if the element is not present in the list.

    Syntax:

    public int indexOf(E e, int index)

    Parameters: The function accepts two parameters which are described below:

    • index: specifies the index from which the occurrence is to be searched.
    • e: specifies the element whose first occurrence from position index is to be returned.

    Return Value: The function returns the first occurrence of the element after position index. It returns -1 if the element is not present in the list.

    Exceptions: The function throws an IndexOutOfBoundsException if the specified index is negative.

    Below programs illustrate the above function:

    Program 1:




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

    Program 2:




    // Java Program to illustrate the CopyOnWriteArrayList
    // indexOf() 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("indexOf value: " 
                          + ArrLis.indexOf(-1, 200));
        }
    }

    Output:

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

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArrayList.html#indexOf-E-int-

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :