class Found{
public static void main(String[] args){
int[] arr={1,2,3,4,5,6,7};
int temp=found(arr,43);
if(temp==-1){
System.out.println("没有该数据");
}
else{
System.out.println(temp);
}
}
//获取key值第一次出现在数组中的位置
public static int found(int arr[],int key){
for(int x=0;x<arr.length;x++){
if(arr[x]==key)
return x;
}
return -1;
}
}
public static void main(String[] args){
int[] arr={1,2,3,4,5,6,7};
int temp=found(arr,43);
if(temp==-1){
System.out.println("没有该数据");
}
else{
System.out.println(temp);
}
}
//获取key值第一次出现在数组中的位置
public static int found(int arr[],int key){
for(int x=0;x<arr.length;x++){
if(arr[x]==key)
return x;
}
return -1;
}
}
本文介绍了一种方法,用于在整数数组中查找特定元素首次出现的位置。通过定义一个函数,可以返回指定元素在数组中的索引,如果未找到,则返回-1。

7771

被折叠的 条评论
为什么被折叠?



