Java Program to Find Index of an Element From an ArrayList

You are currently viewing Java Program to Find Index of an Element From an ArrayList

Java program to find index of an element from an ArrayList

import java.util.ArrayList;

public class SearchAnElement {
  public static void main(String[] args) {
    //ArrayList object 
    ArrayList arrList = new ArrayList();

    //adding elements in the list
    arrList.add("100");
    arrList.add("200");
    arrList.add("300");
    arrList.add("400");
    arrList.add("500");

    //searching element "300"
    int index = arrList.indexOf("300");
    if (index == -1)
      System.out.println("Element is not found in the list");
    else
      System.out.println("Element found @ index: " + index);
  }
}

Output

Element found @ index: 2

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply