The Wayback Machine - https://web.archive.org/web/20240604192754/https://www.geeksforgeeks.org/java-util-arraylist-indexof-java/
Open In App

Java.util.Arraylist.indexOf() in Java

Last Updated : 10 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The indexOf() method of ArrayList 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)
obj : The element to search for.




// Java code to demonstrate the working of
// indexOf in ArrayList
  
// for ArrayList functions
import java.util.ArrayList;
  
public class IndexOfEx {
  public static void main(String[] args) {
       
  // creating an Empty Integer ArrayList
  ArrayList<Integer> arr = new ArrayList<Integer>(5);
  
  // using add() to initialize values
  arr.add(1);
  arr.add(2);
  arr.add(3);
  arr.add(4);
  
  // printing initial value
  System.out.print("The initial values in ArrayList are : ");
  for (Integer value : arr) {
  System.out.print(value);
  System.out.print(" ");
  }  
  
  // using indexOf() to find index of 3
  int pos =arr.indexOf(3);
    
  // prints 2
  System.out.println("\nThe element 3 is at index : " + pos);
  }
    
}   


Output:

The initial values in ArrayList are : 1 2 3 4 
The element 3 is at index : 2

Practical Application : The index functions are mostly useful to determine last or first occurrence of events, for example last occurrence of 6 in a throw of a die, or 1st occurrence of any letter in a name etc.

One more example:




// Java code to demonstrate the application of
// index functions in ArrayList
  
// for ArrayList functions
import java.util.ArrayList;
  
public class AppliIndex {
  public static void main(String[] args) {
       
  // creating an Empty Integer ArrayList
  ArrayList<Integer> arr = new ArrayList<Integer>(10);
  
  // using add() to initialize dice values
  arr.add(1);
  arr.add(2);
  arr.add(4);
  arr.add(6);
  arr.add(5);
  arr.add(2);
  arr.add(6);
  arr.add(1);
  arr.add(6);
  arr.add(4);
  
  // using IndexOf() to find first index of 6
  int pos1 =arr.indexOf(6);
    
  // using lastIndexOf() to find last index of 6
  int pos2 =arr.lastIndexOf(6);
    
  // to balance 0 based indexing
  pos1 = pos1+1;
  pos2 = pos2+1;
    
  // printing first index of 6
  System.out.println("The first occurrence of 6 is  : " + pos1);
    
  // printing last index of 6
  System.out.println("The last occurrence of 6 is  : " + pos2);
    
  }
    
}   


Output:

The first occurrence of 6 is  : 4
The last occurrence of 6 is  : 9


Previous Article
Next Article

Similar Reads

Java.util.LinkedList.indexOf(), lastIndexof() in Java
Linked list library also offers to depict the first and last index of element that has to be found using the indexOf() and lastIndexOf() functions respectively. They offer a variety as the direct access is not available in the conventionally made linked list, hence knowledge of it is useful. 1. indexOf(Object o) : This method returns the index of t
4 min read
Java.util.ArrayList.add() Method in Java
Below are the add() methods of ArrayList in Java: boolean add(Object o) : This method appends the specified element to the end of this list. Parameters: object o: The element to be appended to this list. Exception: NA // Java code to illustrate add(Object o) import java.io.*; import java.util.ArrayList; public class ArrayListDemo { public static vo
2 min read
Java.util.ArrayList.addall() method in Java
Below are the addAll() methods of ArrayList in Java: boolean addAll(Collection c) : This method appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the ope
4 min read
How to clone an ArrayList to another ArrayList in Java?
The clone() method of the ArrayList class is used to clone an ArrayList to another ArrayList in Java as it returns a shallow copy of its caller ArrayList. Syntax: public Object clone(); Return Value: This function returns a copy of the instance of Object. Below program illustrate the Java.util.ArrayList.clone() method: Example: Java Code // Java pr
2 min read
Copy Elements of One ArrayList to Another ArrayList in Java
It is the implementation class of List Interface. It allows duplicated objects/elements and as well as maintains the insertion order. You can get the element present inside the ArrayList by the index of it now you need to pass it into the getting (index) method. You can add the elements into ArrayList using the add() method. Syntax: ArrayList Initi
7 min read
Java String indexOf()
In Java, String indexOf() method returns the position of the first occurrence of the specified character or string in a specified string. Variants of indexOf() Method There are four variants of the indexOf() method are mentioned below: int indexOf()int indexOf(char ch, int strt)int indexOf(String str)int indexOf(String str, int strt)1. int indexOf(
4 min read
LinkedList indexOf() method in Java
The Java.util.LinkedList.indexOf(Object element) method is used to check and find the occurrence of a particular element in the list. If the element is present then the index of the first occurrence of the element is returned otherwise -1 is returned if the list does not contain the element. Syntax: LinkedList.indexOf(Object element) Parameters: Th
2 min read
Vector indexOf() Method in Java
The java.util.vector.indexOf(Object element) method is used to check and find the occurrence of a particular element in the vector. If the element is present then the index of the first occurrence of the element is returned otherwise -1 is returned if the vector does not contain the element. Syntax: Vector.indexOf(Object element) Parameters: Elemen
3 min read
CopyOnWriteArrayList indexOf() method in Java
The indexOf(Object o) method of CopyOnWriteArrayList returns the first occurrence of the element passed in the list. It returns -1 if the element is not present in the list. Syntax: public int indexOf(Object o) Parameters: The function accepts a parameter o whose first occurrence is to be returned. Return Value: The function returns the first occur
3 min read
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