public class ArrayDemo {
public static void main(String[] args) {
int[] arr = { 37, 92, 54, 18, 76 };
// 如何获取数据92在数组中的索引呢
int index = getIndex(arr, 92);
System.out.println(index);
}
// 获取数组中指定元素的索引
public static int getIndex(int[] arr, int value) {
int index = -1;
for (int x = 0; x < arr.length; x++) {
if (arr[x] == value) {
index = x;
break;
}
}
return index;
}
} |
|