how to find the index of an element by value in java array/**
* @author lautturi.com
* Java example: find the index of an element in an array
*/
import java.util.*;
public class Lautturi {
public static void main(String[] args) {
String[] strArray = { "java", "python", "perl", "js", "perl", "c", "go" };
String find = "js";
int idx = -1;
for (int i = 0; i < strArray.length; i++) {
if (strArray[i] == find) {
idx = i;
break;
}
}
if (idx == -1) {
System.out.println("the element is not found");
} else {
System.out.println("The index of element is " + idx);
}
}
}