Search elements in ArrayList in Java

In this tutorial, we will learn how to Search Elements in ArrayList in Java.

ArrayList in Java is a resizable-array implementation of the List interface. ArrayList in Java implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. ArrayList class in Java provides two methods indexOf() and lastIndexOf().

We will use indexOf() and lastIndexOf() methods to search elements in ArrayList in Java. The syntax of indexOf() and lastIndexOf() methods are:

Syntax of indexOf() method:

this method 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 of lastIndexOf() method:

this method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

Example:

Output:

 

Related posts

Leave a Comment