The indexOf() method of ArrayList returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
public int IndexOf(Object o) obj : The element to search for.
The initial values in ArrayList are : 1 2 3 4 The element 3 is at index : 2
Practical Application : The index functions are mostly useful to determine last or first occurrence of events, for example last occurrence of 6 in a throw of a die, or 1st occurrence of any letter in a name etc.
One more example:
The first occurrence of 6 is : 4 The last occurrence of 6 is : 9
This article is contributed by Shambhavi Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java Deprecated API Scanner tool (jdepscan) in Java 9 with Examples
- Java.lang.Short toString() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
Improved By : Akanksha_Rai