获取数组中的最小值,然后获取索引 [英] Get minimum value in an array and then get index

查看:71
本文介绍了获取数组中的最小值,然后获取索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个数组中获取最小值,然后获取该项目的索引,而不用编写自己的循环(如果需要请告诉我),只需一步即可。

I want to get the minimum value in an array and then get the index of that item, in one step without writing my own loop (if I have to please let me know).

我知道我可以做

$ b =($ a |最小-测量) code>

但是我必须做

[array] :: IndexOf($ a,$ b)

而且通常情况下还可以,但是我正在寻找一种方法来做一次,因为我要循环运行很多次。

I know I can just do the
$b = ($a | Measure -Minimum).Minimum
But then I have to do
[array]::IndexOf($a, $b)
And while that is normally okay, I'm looking for a way to do it once because I'm running this MANY MANY times in a loop.

谢谢!

编辑:一步的意思是不用两次遍历数组

One step meaning without looping through the array twice

推荐答案

是类型问题,请尝试这样:

its a problem of type, try like this:

$myArray = [int[]]5,66,4,33,2,9,9,12
$minvalue=[int]($myArray | measure -Minimum).Minimum
$myArray.IndexOf($minvalue)

这篇关于获取数组中的最小值,然后获取索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆