Open In App

List indexOf() Method in Java with Examples

Last Updated : 02 Jan, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Syntax:

public int indexOf(Object o)

Parameters: This function has a single parameter, i.e, the element to be searched in the list.

Returns: This method returns the index of first occurrence of the given element in the list and returns “-1” if element is not in the list.

Below programs show the implementation of this method.

Program 1:




// Java code to show the implementation of
// indexOf method in list interface
import java.util.*;
public class GfG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Initializing a list of type Linkedlist
        List<Integer> l = new LinkedList<>();
        l.add(1);
        l.add(3);
        l.add(5);
        l.add(7);
        System.out.println(l);
        System.out.println(l.indexOf(5));
    }
}


Output:

[1, 3, 5, 7]
2

Program 2: Below is the code to show implementation of list.hashCode() using Linkedlist.




// Java code to show the implementation of
// indexOf method in list interface
import java.util.*;
public class GfG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Initializing a list of type Linkedlist
        List<String> l = new LinkedList<>();
        l.add("10");
        l.add("15");
        l.add("20");
        System.out.println(l);
        System.out.println(l.indexOf("20"));
    }
}


Output:

[10, 15, 20]
2

Reference:
Oracle Docs



Previous Article
Next Article

Similar Reads

Java Guava | Bytes.indexOf(byte[] array, byte target) method with Examples
Bytes.indexOf(byte[] array, byte target) method of Guava's Bytes Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(byte[] array, byte target) Parameters:
3 min read
Java Guava | Floats.indexOf(float[] array, float target) method with Examples
Floats.indexOf(float[] array, float target) method of Guava's Floats Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(float[] array, float target) Param
3 min read
Java Guava | Doubles.indexOf(double[] array, double target) method with Examples
Doubles.indexOf(double[] array, double target) method of Guava's Doubles Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(double[] array, double target)
3 min read
Java Guava | Shorts.indexOf(short[] array, short target) method with Examples
Shorts.indexOf(short[] array, short target) method of Guava's Shorts Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(short[] array, short target) Param
3 min read
Java Guava | Booleans.indexOf(boolean[] array, boolean target) method with Examples
Booleans.indexOf(boolean[] array, boolean target) method of Guava's Booleans Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(boolean[] array, boolean t
3 min read
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(
3 min read
StringBuffer indexOf() method in Java with Examples
In StringBuffer class, there are two types of indexOf() method depending upon the parameters passed to it. indexOf(String str) The indexOf(String str) method of StringBuffer class is used to return the index of the String for first occurrence of passed substring as parameter from the sequence contained by this object. If substring str is not presen
4 min read
StringBuilder indexOf() method in Java with Examples
In StringBuilder class, there are two types of indexOf() method depending upon the parameters passed to it. indexOf(String str) The indexOf(String str) method of StringBuilder class is the inbuilt method used to return the index within the String for first occurrence of passed substring as parameter. If substring str is not present then -1 is retur
4 min read
Java Guava | Booleans.indexOf(boolean[] array, boolean[] target) method with Examples
Booleans.indexOf(boolean[] array, boolean[] target) method of Guava's Booleans Class accepts two parameters array and target. If the target exists within the array, the method returns the start position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(boolean[] array, b
2 min read
Java Guava | Ints.indexOf(int[] array, int[] target) method with Examples
Ints.indexOf(int[] array, int[] target) method of Guava's Ints Class accepts two parameters array and target. If the target exists within the array, the method returns the start position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(int[] array, int[] target) Paramet
3 min read