我有一个双打数组,我想要最高值的索引.这些是我到目前为止提出的解决方案,但我认为必须有一个更优雅的解决方案.想法?
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) 给出这样一个清单:
List<int> intList = new List<int>();
intList.Add(5);
intList.Add(10);
intList.Add(15);
intList.Add(46);
Run Code Online (Sandbox Code Playgroud)
你如何获得列表中最大元素的索引?在这种情况下,它在索引3处.
编辑:遗憾的是标准LINQ没有提供这些功能.