AbstractList indexOf() method in Java with Examples
The indexOf() method of java.util.AbstractList class is used to return the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
Syntax:
public int indexOf(Object o)
Parameters: This method takes Object o as parameter which is the element to search for.
Return Value: This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
Below are the examples to illustrate the indexOf() method.
Example 1:
// Java program to demonstrate // indexOf() method // for Integer value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of AbstractList<Integer> AbstractList<Integer> arrlist1 = new ArrayList<Integer>(); // Populating arrlist1 arrlist1.add(10); arrlist1.add(20); arrlist1.add(30); arrlist1.add(40); arrlist1.add(50); // print arrlist1 System.out.println("ArrayListlist : " + arrlist1); // getting the index of element 30 // using indexOf() method int index = arrlist1.indexOf(30); // print the index System.out.println("index : " + index); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } |
ArrayListlist : [10, 20, 30, 40, 50] index : 2
Example 2:
// Java program to demonstrate // indexOf() method // for Integer value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of AbstractList<Integer> AbstractList<Integer> arrlist1 = new ArrayList<Integer>(); // Populating arrlist1 arrlist1.add(10); arrlist1.add(20); arrlist1.add(30); arrlist1.add(40); arrlist1.add(50); // print arrlist1 System.out.println("ArrayListlist : " + arrlist1); // getting the index of element 25 // using indexOf() method int index = arrlist1.indexOf(25); // print the index System.out.println("Index of 25 : " + index); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } |
ArrayListlist : [10, 20, 30, 40, 50] Index of 25 : -1
Example 3: For Null value
// Java program to demonstrate // indexOf() method // for null value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of AbstractList<Integer> AbstractList<Integer> arrlist1 = new ArrayList<Integer>(); // Populating arrlist1 arrlist1.add(10); arrlist1.add(20); arrlist1.add(30); arrlist1.add(40); arrlist1.add(50); // print arrlist1 System.out.println("ArrayListlist : " + arrlist1); // creating null object Object value = null; // getting the index of element null // using indexOf() method int index = arrlist1.indexOf(value); // print the index System.out.println("Index of null : " + index); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } |
ArrayListlist : [10, 20, 30, 40, 50] Index of null : -1
Recommended Posts:
- AbstractList get() method in Java with Examples
- AbstractList set() Method in Java with Examples
- AbstractList add(E ele) method in Java with Examples
- AbstractList subList() method in Java with Examples
- AbstractList lastIndexOf() method in Java with Examples
- AbstractList iterator() method in Java with Examples
- AbstractList addAll() method in Java with Examples
- AbstractList remove() Method in Java with Examples
- AbstractList hashCode() method in Java with Examples
- AbstractList listIterator() Method in Java with Examples
- AbstractList equals() method in Java with Examples
- AbstractList clear() method in Java with Examples
- AbstractList add(int index, E element) method in Java with Examples
- List indexOf() Method in Java with Examples
- StringBuffer indexOf() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



