

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the index of given element of a Java List?
The indexOf(Object) method of the java.util.ArrayList class returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. Using this method, you can find the index of a given element.
Example
import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList<String> arrlist = new ArrayList<String>(5); arrlist.add("G"); arrlist.add("E"); arrlist.add("F"); arrlist.add("M"); System.out.println("Size of list: " + arrlist.size()); for (String value : arrlist) { System.out.println("Value = " + value); } int retval=arrlist.indexOf("E"); System.out.println("The element E is at index " + retval); } }
Output
Size of list: 4 Value = G Value = E Value = F Value = M The element E is at index 1
- Related Questions & Answers
- C# program to find the index of an element in a List
- How to find what is the index of an element in a list in Python?
- How to find the index of an item given a list containing it in Python?
- How do you get the index of an element in a list in Java?
- How to find the last occurrence of an element in a Java List?
- JavaScript: How to Find the Middle Element of a Linked List?
- Find the tuples containing the given element from a list of tuples in Python
- How to find the index of an element in a vector in R?
- How to remove element from the specified index of the List in C#?
- C program to find the median of a given list.
- Find the last element of a list in scala
- Python – Negative index of Element in List
- How to find the index of an object available in a list in Python?
- How to get the first element of the List in Java?
- How to find an element in a List with Java?
Advertisements