我有一个元素数组。如果我执行 arr.max
,我将获得最大值。但我想获取数组的索引。如何在 Ruby 中找到它
例如
a = [3,6,774,24,56,2,64,56,34]
=> [3, 6, 774, 24, 56, 2, 64, 56, 34]
>> a.max
a.max
=> 774
我需要知道 774
的索引,即 2
。我如何在 Ruby 中执行此操作?
最佳答案
a.index(a.max) should give you want you want
关于ruby - 如何找到具有最大值的数组的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3538627/