ArrayList.indexOf(Object): This method is available in java.util.ArrayList class of Java.
Syntax:
int java.util.ArrayList.indexOf(Object o)
This method takes one argument of type Object as its parameter. This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
Parameters: One parameter is required for this method.
o: the 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.
Exceptions: NA
Approach
Java
import java.util.ArrayList;public class ArrayListindexOf {public static void main(String[] args) {ArrayList<Integer> arr = new ArrayList<>();arr.add(1);arr.add(2);arr.add(3);Integer o = 2;System.out.println(arr.indexOf(o));}}
Output:
1
No comments:
Post a Comment