找到C#中int数组的最小值

编程入门 行业动态 更新时间:2024-10-13 18:26:46
本文介绍了找到C#中int数组的最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我怎么能找到用C#

推荐答案

使用LINQ在INT ARRY min值:

With LINQ:

int min = theArray.Min();

注意,如果没有这将错误的任何的元素;检查。长度第一(或者,项目 INT?,但会增加开销)。

note that this will error if there aren't any elements; check the .Length first (or alternatively, project to int?, but that adds overhead).

如果你没有可用的LINQ,那么也许:

If you don't have LINQ available, then perhaps:

public static int Min(int[] arr) { switch (arr.Length) { case 0: throw new InvalidOperationException(); case 1: return arr[0]; case 2: return Math.Min(arr[0], arr[1]); default: int min = arr[0]; for (int i = 1; i < arr.Length; i++) { if (arr[i] < min) min = arr[i]; } return min; } }

更多推荐

找到C#中int数组的最小值

本文发布于:2023-11-07 10:40:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566231.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   最小值   int

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!