![](/img/trans.png)
[英]How to select the largest index of the smallest element in a single array Java
[英]How do I get the index of the smallest element in a double array?
如何在Java中獲取雙數組中最小元素的索引double[]
?
迭代數組,比較不同的元素,始終存儲最小值的值和索引,以便進行比較和檢索。
保持到目前為止看到的最低值,以及該值的索引。
查看數組,如果該點的值較低,則使用該值作為從現在開始的最低值,並更新保持的索引。
更短,給你代碼。
public int minIndex(double... ds) {
int idx = -1;
double d= Double.POSITIVE_INFINITY;
for(int i = 0; i < ds.length; i++)
if(ds[i] < d) {
d = ds[i];
idx = i;
}
return idx;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.