相关疑难解决方法(0)

C#找到最高的数组值和索引

所以我有一个未排序的数值数组int[] anArray = { 1, 5, 2, 7 };,我需要得到数组中最大值的值和索引,这将是7和3,我该怎么做?

c# arrays indexing

74
推荐指数
5
解决办法
24万
查看次数

如何使用LINQ获取数组中最高值的索引?

我有一个双打数组,我想要最高值的索引.这些是我到目前为止提出的解决方案,但我认为必须有一个更优雅的解决方案.想法?

double[] score = new double[] { 12.2, 13.3, 5, 17.2, 2.2, 4.5 };
int topScoreIndex = score.Select((item, indx) => new {Item = item, Index = indx}).OrderByDescending(x => x.Item).Select(x => x.Index).First();

topScoreIndex = score.Select((item, indx) => new {Item = item, Index = indx}).OrderBy(x => x.Item).Select(x => x.Index).Last();

double maxVal = score.Max();
topScoreIndex = score.Select((item, indx) => new {Item = item, Index = indx}).Where(x => x.Item == maxVal).Select(x => x.Index).Single();
Run Code Online (Sandbox Code Playgroud)

c# linq

22
推荐指数
3
解决办法
3万
查看次数

标签 统计

c# ×2

arrays ×1

indexing ×1

linq ×1