public static void main(String[] args) {
/*String [] ac = {"sche"," ","1123","we"};
String a = "we";
for(int i=0;i< ac.length;i++){
if(ac[i]==a){
System.out.println("索引位置是:"+i);
break;
}
}*/
/*
* 查找数组中指定元素第一次出现的索引值。
int[] arr = {98,23,16,35,72};
查找23在数组中的索引值。
*/
System.out.println(getIndex(23));
}
private static int getIndex(int x) {
int[] arr = {98,23,16,35,72};
for (int i = 0; i < arr.length; i++) {
if (arr[i] == x) {
return i;
}
}
return -1; //若数组中没有则返回-1
}
最后
以上就是陶醉小甜瓜最近收集整理的关于数组元素查找(查找指定元素第一次在数组中出现的索引)的全部内容,更多相关数组元素查找(查找指定元素第一次在数组中出现内容请搜索靠谱客的其他文章。
发表评论 取消回复