🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
ArrayList.indexOf() method in Java is used to find the index of the first occurrence of a specified element in an ArrayList. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
indexOfMethod Syntax- Examples
- Finding the Index of an Element
- Handling Elements Not Found in the List
- Real-World Use Case
- Conclusion
Introduction
The indexOf() method is a member of the ArrayList class in Java. It is used to search for an element in the list and returns the index of the first occurrence of that element. If the element is not found, the method returns -1.
indexOf Method Syntax
The syntax for the indexOf method is as follows:
public int indexOf(Object o)
- o: The element to search for in the list.
The method returns the index of the first occurrence of the specified element, or -1 if the element is not found.
Examples
Finding the Index of an Element
The indexOf method can be used to find the index of the first occurrence of a specified element in the ArrayList.
Example
import java.util.ArrayList;
import java.util.List;
public class IndexOfExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
// Find the index of "Banana"
int index = list.indexOf("Banana");
System.out.println("Index of Banana: " + index);
}
}
Output:
Index of Banana: 1
Handling Elements Not Found in the List
If the specified element is not found in the ArrayList, the indexOf method returns -1.
Example
import java.util.ArrayList;
import java.util.List;
public class IndexOfNotFoundExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
// Try to find the index of "Grapes"
int index = list.indexOf("Grapes");
System.out.println("Index of Grapes: " + index);
}
}
Output:
Index of Grapes: -1
Real-World Use Case
Checking User Roles
In an application with user roles, you might want to check if a specific role exists in a list of user roles. The indexOf method can be used to determine the index of a particular role.
Example
import java.util.ArrayList;
import java.util.List;
public class UserRoleCheck {
public static void main(String[] args) {
List<String> userRoles = new ArrayList<>();
userRoles.add("Admin");
userRoles.add("Editor");
userRoles.add("Viewer");
// Check if the "Editor" role exists
int index = userRoles.indexOf("Editor");
if (index != -1) {
System.out.println("Role 'Editor' found at index: " + index);
} else {
System.out.println("Role 'Editor' not found.");
}
}
}
Output:
Role 'Editor' found at index: 1
Conclusion
The ArrayList.indexOf() method in Java is a simple and effective way to find the index of the first occurrence of a specified element in an ArrayList. By understanding how to use this method, you can efficiently search for elements and handle cases where elements are not found in your Java applications. Whether you are searching for specific data, checking user roles, or handling other list-related tasks, the indexOf method provides a reliable solution.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment