public class Test4_2 {
public static void main(String[] args) {
int [] arr={11,33,44,55,11,11,};
int[] result = getIndex(11,arr);
if (result.length==0){
System.out.println("抱歉,你输入的元素有误!");
}else{
for (int i = 0; i< result.length; i++) {
System.out.println(result[i]);
}
}
}
/*
设计一个方法,查找数组中元素所在的索引位置(考虑元素重复问题)
*/
public static int[] getIndex(int num,int [] arr){
int count=0;
for (int i = 0; i < arr.length; i++) {
if (arr[i]==num){
count++;
}
}
int [] result=new int[count];
int index=0;
for (int i = 0; i < arr.length; i++) {
if (arr[i]==num){
result [index]=i;
index++;
}
}
return result;
}
}
标签:11,arr,重复,++,索引,int,查找,result,length
From: https://blog.51cto.com/u_15988249/6218479