Java 找到最小数字的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20332199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find smallest number's index
提问by user3057751
It's returning the second element in the array instead of the smallest number's index
I already took the size and all that stuff, this is just the method
它返回数组中的第二个元素而不是最小数字的索引
我已经采用了大小和所有这些东西,这只是方法
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
回答by Alexis C.
How would you do on paper ?
在纸上你会怎么做?
- Initialize the minimum value with the first element of your array
- Initialize the corresponding index to 0 (arrays are 0 base indexed)
- Loop in your array
- If you find a number smaller than the minimum value, update the minimum value with the value found
- If 4 is satisfied, update the corresponding index with the current index
- Return the index
- You've done it.
- 用数组的第一个元素初始化最小值
- 将对应的索引初始化为 0(数组是 0 基索引的)
- 在你的数组中循环
- 如果您发现一个数字小于最小值,则用找到的值更新最小值
- 如果满足4,则用当前索引更新对应的索引
- 返回索引
- 你已经做到了。
回答by EthioDroid
Your problem is what you return.
你的问题是你返回什么。
A couple of things:
几件事:
The array shouldn't be static, you should pass it as a parameter to the arrayMin method; min should be a local arrayMin variable, not static; min should be initialized to Integer.MAX_VALUE. If you initialize it with 1, and 2 happens to be the min value of the array, you'll never return it; You can't return multiple times from a method. As soon as you do return min, the method ends. There's probably some confusion over the the variable min will return the smallest number from the first i elements phrase. It probably means that in each iteration, the variable min will have (not return) the smallest number from the first i elements.
数组不应该是静态的,你应该将它作为参数传递给 arrayMin 方法;min 应该是一个局部的 arrayMin 变量,而不是静态的;min 应初始化为 Integer.MAX_VALUE。如果你用 1 初始化它,而 2 恰好是数组的最小值,你将永远不会返回它;您不能从一个方法多次返回。只要您返回 min,该方法就结束。可能对变量 min 会返回第 i 个元素短语中的最小数字存在一些混淆。这可能意味着在每次迭代中,变量 min 将具有(不返回)前 i 个元素中的最小数字。
Here's a refactor:
这是一个重构:
public static int arrayMin(int[] arr1) {
int i = 0;
int min = Integer.MAX_VALUE;
if (arr1 == null) {
return 0; // What if 0 is the minimum value? What do you want to do in this case?
} else {
while (i < arr1.length) {
if (arr1[i] < min) {
min = arr1[i];
}
i++;
}
}
return min;
}
回答by Luke
public static int FindSmallest (int [] arr1) {
int index = 0;
int min = arr1[index];
for (int i=1; i<arr1.length; i++) {
...
if (arr1[i] < min) {
min = arr1[i];
index = i;
}
...
}
return index;
}
回答by Rakesh KR
Algorithm FindSmallest (arr1[])
// This Algorithm returns the index of smallest element in the array arr1
// Assumption : index of arr1 starts from 0
// arr1.length returns the length of arr1
begin
set index := 0;
set len := arr1.length;
set min := arr1[index];
For i:=1 to len,do
begin
if arr1[i] < min ,then
begin
min := arr1[i];
index := i;
end
end
return index;
end