BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java ArrayList indexOf() Method example

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

Java.util.ArrayList class method indexOf(Object o) is used to find out the index of a particular element in a list.

Method indexOf() Signature

public int indexOf(Object o)

This method returns -1 if the specified element is not present in the list.

ArrayList indexOf() Method example

In the following example we have an arraylist of strings and we have added few elements to the arraylist. Here we are using the indexOf() method to find the index of few specified elements in the ArrayList.

import java.util.ArrayList;
public class IndexOfExample {
  public static void main(String[] args) {
      ArrayList<String> al = new ArrayList<String>();
      al.add("AB");
      al.add("CD");
      al.add("EF");
      al.add("GH");
      al.add("IJ");
      al.add("KL");
      al.add("MN");

      System.out.println("Index of 'AB': "+al.indexOf("AB"));
      System.out.println("Index of 'KL': "+al.indexOf("KL"));
      System.out.println("Index of 'AA': "+al.indexOf("AA"));
      System.out.println("Index of 'EF': "+al.indexOf("EF"));
  }
}

Output:

Index of 'AB': 0
Index of 'KL': 5
Index of 'AA': -1
Index of 'EF': 2

As you can see in the output that the index of element ‘AA’ is returned as -1 because this element does not exist in the ArrayList.

ArrayList indexOf() Method example if duplicate elements are present

In the following example, the element 90 is present twice in ArrayList, at the index 0 and index 3. When we used the indexOf() method to find the index of 90, we got the index of first occurrence of 90, this is because this method returns the index of first occurrence of the specified element. If you want to find out the last occurrence of the specified element use lastIndexOf() method instead.

import java.util.ArrayList;
public class JavaExample {
   public static void main(String[] args) {
      ArrayList<Integer> al = new ArrayList<Integer>();
      al.add(90);
      al.add(15);
      al.add(20);
      al.add(90);

      System.out.println("Index of 90: "+al.indexOf(90));
      System.out.println("Index of 20: "+al.indexOf(20));
      System.out.println("Index of 15: "+al.indexOf(15));
   }
}

Output:
Java ArrayList indexOf() Method example

Reference:

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#indexOf(java.lang.Object)

Top Related Articles:

  1. How to loop HashMap in java
  2. Java String equals() and equalsIgnoreCase() Methods example
  3. How to Convert an array to ArrayList in java
  4. Java StringBuffer lastIndexOf()
  5. Java StringBuilder ensureCapacity()

Tags: Collections, Java-ArrayList

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Divya says

    March 9, 2018 at 5:04 PM

    If I want to take the ekement from user and for that element I want to find the index??
    Its something like this
    String s=s.next();
    I want to find the index for s

    Reply
    • Chaitanya Singh says

      October 6, 2019 at 5:52 AM

      Yeah you can do that. Just pass the s in the method like this: int pos = al.indexOf(s);

      Reply
  2. rohith says

    July 7, 2018 at 8:40 AM

    can we print the indices of all the elements present in array list?

    Reply
    • Chaitanya Singh says

      October 6, 2019 at 5:50 AM

      Yeah we can but why would you want to do that? The purpose of indexOf() method is to find an element in a List, what is the point to find the position of all the elements. You can print an entire ArrayList if you want.

      Reply
  3. shruti says

    February 4, 2019 at 1:49 PM

    Get the each index value of 10 present in below list
    Input: List = 10,20,30,90,10,10,40,50,10

    Reply
    • Chaitanya Singh says

      October 6, 2019 at 5:47 AM

      It will be 0 because the indexOf() method returns the index of first occurrence of the element.

      Reply
  4. zubair ahmed says

    October 4, 2019 at 5:54 PM

    well how come index of aa is -1…….
    you can check out my code..

    ArrayList list1 = new ArrayList(3);
    String x= list1.get(0);
    int i= list1.indexOf(x);
    System.out.println(i)
    which will give you 0…

    Reply
    • Chaitanya Singh says

      October 6, 2019 at 5:44 AM

      Your code gave you 0 because you are finding the index of the first element. The index of first element is 0 in ArrayList. The example which I have shared above returns -1 for element ‘AA’ because the element doesn’t exist in the ArrayList. The indexOf() method returns -1 if the element is not present in the List.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap