package day02;
public class Array_Demo {
//查找指定元素,第一次出现在数组中的索引位置
public static void main(String[] args) {
int[] arr = {11, 22, 5, 44, 55};
int Index=getIndex(arr,12);//调用方法
System.out.println(Index);//输出返回值
}
public static int getIndex(int[] arr, int value) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
return i;
}
}
return -1;//如果未找到返回-1
}
}
java:查找某个元素在数组中出现的索引位置
猜你喜欢
转载自blog.csdn.net/qq_24644517/article/details/81567973
今日推荐
周排行