Press "Enter" to skip to content

ArrayList indexOf() Method in Java

ArrayList indexOf() method returns the index of the first occurrence of the specified element from the list or -1 if this list does not contain the element. Technically, it gives back the lowest index of the element from the list.

You can call this method will null or custom object to get their index. The time complexity of this method is O(n).

Syntax:

public int indexOf(Object o)
Freshers Placement

Parameters – o is the element to search.
Returns – the first index of the element if found otherwise -1.

Let’s try to visualize the indexOf() operation using a diagram.

ArrayList indexof

ArrayList indexOf() Example

Let’s see how to use the indexOf(Element e) to search the element in the ArrayList. The index returned can never be greater than (ArrayList size() – 1).

import java.util.ArrayList;

public class ArrayListDemo {
	public static void main(String[] args) {
		ArrayList<Integer> numbers = new ArrayList<>();
		numbers.add(11);
		numbers.add(87);
		numbers.add(17);
		numbers.add(83);
		numbers.add(17);

		System.out.println("ArrayList element: " + numbers);
		System.out.println("Index of 17 is: " + numbers.indexOf(17));
	}
}

Output:

ArrayList element: [11, 87, 17, 83, 17]
Index of 17 is: 2

ArrayList indexOf() returns -1 if element is not present

import java.util.ArrayList;

public class ArrayListDemo {

	public static void main(String[] args) {
		ArrayList<String> countries = new ArrayList<>();
		countries.add("India");
		countries.add("Australia");
		countries.add("Africa");
		countries.add("Srilanka");

		System.out.println("ArrayList element: " + countries);
		System.out.println("Index of Nepal is: " + countries.indexOf("Nepal"));
	}
}

Output:

ArrayList element: [India, Australia, Africa, Srilanka]
Index of Nepal is: -1

Use of indexOf() with Custom Object

Freshers Placement

You can also find the index of the user-defined object. Let’s have a look at the program.

class Employee {
	private int id;
	private String name;

	public Employee() {
	}

	public Employee(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	@Override
	public String toString() {
		return "Employee [employeeId=" + id + ", employeeName=" + name + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Employee other = (Employee) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

}
public class ArrayListDemo {
	public static void main(String[] args) {
		List<Employee> employees = new ArrayList<>();
		employees.add(new Employee(1, "Sachin"));
		employees.add(new Employee(2, "Stevan"));
		employees.add(new Employee(3, "Kenneth"));
		employees.add(new Employee(4, "Brian"));

		Employee employee = new Employee(3, "Kenneth");
		// finds the index of Kenneth
		System.out.println("Index of Kenneth is: " + employees.indexOf(employee));
	}
}

That’s all in this article. Please share it if you found this helpful. You can also find the lastIndexOf() of the element from the ArrayList.

Further Reference:

ArrayList Reference Page.

Comments are closed.