
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How do you get the index of an element in a list in Java?
indexOf() method of List is used to get the location of an element in the list.
int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
Parameters
o − Element to search for.
Returns
The index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
Throws
ClassCastException − If the type of the specified element is incompatible with this list (optional).
NullPointerException − If the specified element is null and this list does not permit null elements (optional).
Example
Following is the example showing the usage of indexOf() method −
package com.tutorialspoint; import java.util.ArrayList; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Zara"); list.add("Mahnaz"); list.add("Ayan"); System.out.println("List: " + list); String student = "Ayan"; String missingStudent = "Aman"; System.out.println("Ayan is present at: " + list.indexOf(student)); System.out.println("Aman index: " + list.indexOf(missingStudent)); } }
Output
This will produce the following result −
List: [Zara, Mahnaz, Ayan] Ayan is present at: 2 Aman index: -1
- Related Articles
- How do you add an element to a list in Java?
- How do you check if an element is present in a list in Java?
- Get the index of a particular element in an ArrayList in Java
- How do you copy an element from one list to another in Java?
- Get the last index of a particular element in an ArrayList in Java
- How do you remove an array element by its index in MongoDB
- How do I find an element in Java List?
- How do you create an empty list in Java?
- How do you check a list contains an item in Java?
- How do you search for an element in an ArrayList in Java?
- How do you find the element of a LinkedList in Java?
- How do you copy a list in Java?
- How do you create a list in Java?
- How do I add an element to an array list in Java?
- How to find the index of given element of a Java List?
