C# 找到最高的数组值和索引 [英] C# find highest array value and index

查看:81
本文介绍了C# 找到最高的数组值和索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I have an unsorted numeric array int[] anArray = { 1, 5, 2, 7 }; and I need to get both the value and the index of the largest value in the array which would be 7 and 3, how would I do this?

推荐答案

这不是最迷人的方式,但有效.

This is not the most glamorous way but works.

(必须有using System.Linq;)

 int maxValue = anArray.Max();
 int maxIndex = anArray.ToList().IndexOf(maxValue);

这篇关于C# 找到最高的数组值和索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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