它返回数组中的第二个元素,而不是最小数字的索引
我已经确定了尺寸和所有这些东西,这只是方法
public static int FindSmallest (int [] arr1){//start method
int index = arr1[0];
for (int i=1; i<arr1.length; i++){
if (arr1[i] > index ){
index = arr1[i];
}
return index ;
}
return 0;
}//end method
最佳答案
你在纸上会怎样做?
- 使用数组的第一个元素初始化最小值
- 将相应索引初始化为 0(数组以 0 为基索引)
- 循环数组
- 如果发现小于最小值的数字,请使用找到的值更新最小值
- 如果满足4,则用当前索引更新对应索引
- 返回索引
- 你已经做到了。
关于java - 找到最小数字的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20332199/