beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java ArrayList indexOf() Method example

By Chaitanya Singh | Filed Under: Java Collections

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)

Enjoyed this post? Try these related posts

  1. Java ArrayList contains() Method example
  2. Java ArrayList set() Method example
  3. Queue Interface in Java Collections
  4. Java ArrayList addAll(int index, Collection c) Method example
  5. Hashtable in java with example
  6. How to synchronize ArrayList in java with example

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 *

ArrayList Tutorial

  • Java ArrayList

Java ArrayList Methods

  • add(Object obj)
  • add(int index, Object element)
  • addAll(Collection c)
  • addAll(int index, Collection c)
  • contains()
  • get()
  • indexOf()
  • ensureCapacity()
  • isEmpty()
  • lastIndexOf()
  • remove()
  • remove(Object obj)
  • trimToSize()
  • set()
  • clone()
  • clear()
  • size()

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap