java.util.Arrays.asList(theArray).indexOf(o)
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
// create an ArrayList
ArrayList<Integer> numbers = new ArrayList<>();
// insert element to the arraylist
numbers.add(22);
numbers.add(13);
numbers.add(35);
System.out.println("Number ArrayList: " + numbers);
// find the position of 13
int position1 = numbers.indexOf(13);
System.out.println("Index of 13: " + position1);
// find the position of 50
int position2 = numbers.indexOf(50);
System.out.println("Index of 50: " + position2);
}
}
// Java program to find index of
// an element in N elements
import java.util.stream.IntStream;
public class index {
// Function to find the index of an element
public static int findIndex(int arr[], int t)
{
int len = arr.length;
return IntStream.range(0, len)
.filter(i -> t == arr[i])
.findFirst() // first occurrence
.orElse(-1); // No element found
}
public static void main(String[] args)
{
int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
System.out.println("Index position of 5 is: "
+ findIndex(my_array, 5));
System.out.println("Index position of 7 is: "
+ findIndex(my_array, 7));
}
}
public int getArrayIndex(int[] arr,int value) {
int k=0;
for(int i=0;i<arr.length;i++){
if(arr[i]==value){
k=i;
break;
}
}
return k;
}