- Find element in ArrayList using indexOf lastIndexOf methods example
- How to find an element in ArrayList using the indexOf lastIndexOf methods in Java?
- How to find element index in ArrayList of custom class objects?
- Java ArrayList indexOf() Method example
- Method indexOf() Signature
- ArrayList indexOf() Method example
- ArrayList indexOf() Method example if duplicate elements are present
- Reference:
- Top Related Articles:
- About the Author
- Comments
- How to get index of object in arraylist java
- index(object o) method
- lastIndexOf(Object o) method
- Leave a Comment Cancel reply
Find element in ArrayList using indexOf lastIndexOf methods example
Find an element in ArrayList using indexOf lastIndexOf methods example shows how to find element in ArrayList using indexOf and lastIndexOf methods in Java. It also shows how to find an object of a custom class in ArrayList by implementing equals and hashCode methods.
How to find an element in ArrayList using the indexOf lastIndexOf methods in Java?
The ArrayList indexOf method returns the index of an element in the ArrayList object.
This method returns the index of the first occurrence of the specified object in the ArrayList. If the specified object is not found, the indexOf method returns -1.
Similarly, use the lastIndexOf method of the ArrayList class to find an element’s last index.
This method returns the index of the last occurrence of the specified object in the ArrayList. If the specified object is not found in the ArrayList, this method returns -1.
How to find element index in ArrayList of custom class objects?
Consider below given example which uses an ArrayList of custom class objects.
Even if the object was in the ArrayList, the indexOf method could not find it and returned -1. That is because the indexOf and lastIndexOf methods rely on the equals method to compare the objects and since we have not implemented the equals method in the Customer class, it could not find it.
Here is the updated version of Customer class which implements equals and hashCode methods.
Java ArrayList indexOf() Method example
Java.util.ArrayList class method indexOf(Object o) is used to find out the index of a particular element in a list.
Method indexOf() Signature
public int indexOf(Object o)
This method returns -1 if the specified element is not present in the list.
ArrayList indexOf() Method example
In the following example we have an arraylist of strings and we have added few elements to the arraylist. Here we are using the indexOf() method to find the index of few specified elements in the ArrayList.
import java.util.ArrayList; public class IndexOfExample < public static void main(String[] args) < ArrayListal = new ArrayList(); al.add("AB"); al.add("CD"); al.add("EF"); al.add("GH"); al.add("IJ"); al.add("KL"); al.add("MN"); System.out.println("Index of 'AB': "+al.indexOf("AB")); System.out.println("Index of 'KL': "+al.indexOf("KL")); System.out.println("Index of 'AA': "+al.indexOf("AA")); System.out.println("Index of 'EF': "+al.indexOf("EF")); > >
Index of 'AB': 0 Index of 'KL': 5 Index of 'AA': -1 Index of 'EF': 2
As you can see in the output that the index of element ‘AA’ is returned as -1 because this element does not exist in the ArrayList.
ArrayList indexOf() Method example if duplicate elements are present
In the following example, the element 90 is present twice in ArrayList, at the index 0 and index 3. When we used the indexOf() method to find the index of 90, we got the index of first occurrence of 90, this is because this method returns the index of first occurrence of the specified element. If you want to find out the last occurrence of the specified element use lastIndexOf() method instead.
import java.util.ArrayList; public class JavaExample < public static void main(String[] args) < ArrayListal = new ArrayList(); al.add(90); al.add(15); al.add(20); al.add(90); System.out.println("Index of 90: "+al.indexOf(90)); System.out.println("Index of 20: "+al.indexOf(20)); System.out.println("Index of 15: "+al.indexOf(15)); > >
Output:
Reference:
Top Related Articles:
About the Author
I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.
Comments
If I want to take the ekement from user and for that element I want to find the index??
Its something like this
String s=s.next();
I want to find the index for s
Yeah we can but why would you want to do that? The purpose of indexOf() method is to find an element in a List, what is the point to find the position of all the elements. You can print an entire ArrayList if you want.
well how come index of aa is -1…….
you can check out my code.. ArrayList list1 = new ArrayList(3);
String x= list1.get(0);
int i= list1.indexOf(x);
System.out.println(i)
which will give you 0…
Your code gave you 0 because you are finding the index of the first element. The index of first element is 0 in ArrayList. The example which I have shared above returns -1 for element ‘AA’ because the element doesn’t exist in the ArrayList. The indexOf() method returns -1 if the element is not present in the List.
How to get index of object in arraylist java
As we know ArrayList in java works based on the index and each value stores in a unique index. There may be a situation when we want to find the index of an object. Then we can use the indexOf(Object o) and lastIndexOf(Object o). Lets see how to use java arraylist indexof() method.
Here is the table content of the article will we will cover this topic.
1. index(object o) method
2. lastIndexOf(Object o) method
index(object o) method
The index(Object o) method is used to get the index of the specified element. Suppose if you have duplicate elements in ArrayList then it returns the index value of first occurrence of the element. It returns the int value and if the element doesn’t exist in the ArrayList it returns -1.
Sntytax of java arraylist indexof() method
Where, Object represents the type of elements in ArrayList .
o, is the element which you want to find in ArrayList .
import java.util.ArrayList; import java.util.Iterator; public class ArrayListExample < public static void main(String[] args) < ArrayListlistOfNumbers = new ArrayList(); listOfNumbers.add(111); listOfNumbers.add(222); listOfNumbers.add(333); listOfNumbers.add(444); listOfNumbers.add(555); System.out.println("Index value of 111: "+ listOfNumbers.indexOf(111)); System.out.println("Index value of 555: "+ listOfNumbers.indexOf(555)); System.out.println("Index value of 123: "+ listOfNumbers.indexOf(123)); > >
Output:Index value of 111: 0
Index value of 555: 4
Index value of 123: -1
The index value of ArrayList start from 0 and ends to length-1. Here we have 5 elements in ArrayList from index 0 to 4. So when we are getting first element 111 then it returning index value 0 and for 555 it returning 4.
lastIndexOf(Object o) method
The lastIndexOf(Object o) method is also used to find the index of element but it returns the last index of the specified element. It returns the index value of the last occurrence of the element. It returns the int value. If the element doesn’t exist in ArrayList it returns -1.
Where, Object represents the type of elements in ArrayList.
o, is the element which you want to find in ArrayList.
package package1; import java.util.ArrayList; import java.util.Iterator; public class ArrayListExample < public static void main(String[] args) < ArrayListlistOfNumbers = new ArrayList(); listOfNumbers.add(111); listOfNumbers.add(222); listOfNumbers.add(333); listOfNumbers.add(444); listOfNumbers.add(555); listOfNumbers.add(111); listOfNumbers.add(444); listOfNumbers.add(111); System.out.println("Last Index value of 111: "+ listOfNumbers.lastIndexOf(111)); System.out.println("Last Index value of 444: "+ listOfNumbers.lastIndexOf(444)); System.out.println("Last Index value of 123: "+ listOfNumbers.lastIndexOf(123)); > >
Output: Last Index value of 111: 7
Last Index value of 444: 6
Last Index value of 123: -1
As you can see the element 111 is presented at three positions 0, 5, 7. But this method returns only last position of the element.
Leave a Comment Cancel reply
You must be logged in to post a comment.
- Basics Of Java
- What is JAVA language
- JAVA features/advantage
- How to run Java program
- Difference between JRE, JDK and JVM
- Java Virtual Machine(JVM) & JVM Architecture
- Variables in Java
- Local Variable Type Inference (Java var)
- Java control statements
- if statement in Java
- Switch statement Java
- class and object in Java
- Constructor Overloading in Java
- Constructor chaining in java
- Multiple inheritance using interface in java
- Method overloading in Java
- Method overriding in Java
- Abstract class in java
- Interface in java
- Multiple inheritance using interface in java
- Java import package
- final variable in Java
- final method in java
- final class in Java
- Static variable in java
- Static method in java
- Static block in java
- Static class in java
- Thread life cycle in java
- Thread scheduler in java
- Java Thread Sleep
- join() method in java
- yield() method in java
- wait() method in Java
- notify() method in Java
- Thread class in java
- Daemon thread in java
- Callable and Future in java
- What is immutable String in java
- What is a mutable string
- String concatenation
- String comparison in Java
- Substring in Java
- Convert string to int
- String Replace in Java
- Substring in Java
- String constructor in java
- string methods in java
- try and catch java block
- finally block in java
- throw and throws keyword in java
- Chained exception in java
- User defined exception in java
- Java try with resource
- ArrayList in java
- Java ArrayList methods
- How to add the element in ArrayList
- How to replace element in an ArrayList?
- How to remove element from arraylist java
- How to access ArrayList in java
- How to get index of object in arraylist java
- How to Iterate list in java
- How to achieve Java sort ArrayList
- How to get java sublist from ArrayList
- How to Convert list to array in java
- How to convert array to list java
- How to remove duplicates from ArrayList in java
- Difference between ArrayList and array
- Difference between ArrayList and LinkedList
- Java linked list methods
- How to do LinkedList insertion?
- How to perform deletion in linked list
- How to get the element from LinkedList
- Traverse a linked list java
- Searching in linked list
- How to convert linked list to array in java
- How to remove duplicates from linked list
- How to perform linked list sorting
- Difference between ArrayList and LinkedList
- How to add element in HashSet?
- How to remove elements from HashSet?
- TreeSet internal working
- TreeSet Methods in Java
- Internal working of HashMap
- HashMap method in java
- How to add an object in HashMap by HashMap put() method
- How to get values from hashmap in java example
- How to remove the element by Java HashMap remove() method
- How to replace the value by HashMap replace() method
- How to check the map contains key by hashmap containskey() method
- How to get java map keyset by hashmap keyset() method
- How to get java map entryset by java entryset() method
- How to iterate hashmap in java
- TreeMap(Comparator comp)
- Treemap methods
- Generic types in Java
- Advantages of generics in java
- toString() method in Java
- equals() method in java
- Java hashCode() method
- clone() method in java
- finalize() method in java
- getclass() method in java
- wait() method in Java
- notify() method in Java
- Garbage collector in java
- finalize() method in java
- equals() method in java
- Java hashCode() method
- Comparable java interface
- Comparator interface
- Difference between comparable and comparator
- Why Comparable and Comparator are useful?
- comparator() method in java
- functional interface in java 8
- Predicate in java 8
- Why predicate in java 8
- Why consumer in java 8
- How lambda expression works in java
- Java 8 lambda expression example
- Why Lambda expression use functional interface only
- Lambda expression with the return statement
- Java stream operations in java
- Intermediate operation in Java 8
- Terminal operations in java 8
- Short circuit operations in Java 8
- Lazy evaluation of stream
- Converting stream to collections and Arrays
- Java 9 module
- try with resource improvement
- Optional Class Improvements
- Private methods in interface
- Factory Methods for Immutable List, Set, Map and Map.Entry
- Jshell java 9
- Java CompletableFuture API Improvements
- Local Variable Type Inference (Java var)
- Java 11 String New Methods
Content copy is strictly prohibited. Copyright © by JavaGoal 2022. Designed & Developed By Finalrope Soft Solutions Pvt. Ltd.