Java indexof arraylist – Java ArrayList indexOf() Method with Example

Java indexof arraylist: In the previous article we have discussed about Java ArrayList size() Method with Example

In this article we are going to see the use Java ArrayList indexOf() method along with suitable examples.

Java ArrayList indexOf() Method with Example

indexOf( ):

Arraylist indexof java: This java.util.ArrayList.indexOf() method is used to returns the position of the specific element in the arraylist.

If the element is present then it retrain the position of the element but if the element is not present then it returns ‘-1’

If the type of the element is not compatible with the type of ArrayList then it shows ClassCastException.

If there is no element in the ArrayList and we are still trying to find the indexOf() method then it will show NullPointerException.

Syntax:

arrayListName.indexOf()

Where,

  • arrayListName refers to the name of your ArrayList.

Let’s see different examples to understand it more clearly.

Method-1: Java ArrayList indexOf() Method – Example with String Type ArrayList

Approach:

  • Create a new ArrayList of type String.
  • Add string elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Now get the element specific position in the ArrayList using indexOf( ) method.
  • Print the position.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create an ArrayList of string datatype
        ArrayList<String> arr = new ArrayList<String>();
        // Adding some elements to the ArrayList
        arr.add("tiger");
        arr.add("bear");
        arr.add("lion");
        arr.add("wolf");
        arr.add("leopard");
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr);
        // get the element’s position from arrayList
       System.out.println("Index of Hello: " + arr.indexOf("tiger"));
       System.out.println("Index of hello: " + arr.indexOf("Tiger"));
    }
}
Output:

The elements of ArrayList are: [tiger, bear, lion, wolf, leopard]
Index of Hello: 0
Index of hello: -1

Method-2: Java ArrayList indexOf() Method – Example with Integer Type ArrayList

Approach:

  • Create a new ArrayList of type Integer.
  • Add Integer elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Now get the element specific position in the ArrayList using indexOf( ) method.
  • Print the position.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create an ArrayList of Integer datatype
        ArrayList<Integer> arr = new ArrayList<Integer>();
        // Adding some elements to the ArrayList
        arr.add(13);
        arr.add(52);
        arr.add(21);
        arr.add(101);
        arr.add(1);
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr);
        // get the element’s position from arrayList
       System.out.println("Index of 52: " + arr.indexOf(52));
       System.out.println("Index of 99: " + arr.indexOf(99));
    }
}
Output:

The elements of ArrayList are: [13, 52, 21, 101, 1]
Index of 52: 1
Index of 99: -1

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Related Java Programs: