C#获取一个数组中的最大值、最小值、平均值

C#获取一个数组中的最大值、最小值、平均值

1.给出一个数组

1             int[] array = new int[] { 1,2,4,3,0,-1,34,545,2,34};

2.数组Array自带方法

本身是直接可以调用Min(),Max(),Average()方法来求出 最小值、最大值、平均值

1             Console.WriteLine("--------------Array自身方法-----------------");
2             Console.WriteLine("Min:{0}",array.Min());
3             Console.WriteLine("Max:{0}", array.Max());
4             Console.WriteLine("Average:{0}", array.Average());

输出结果:

1 --------------Array自身方法-----------------
2 Min:-1
3 Max:545
4 Average:62.4

3.编码实现

最小值

 1         /// <summary>
 2         /// 最小值
 3         /// </summary>
 4         /// <param name="array"></param>
 5         /// <returns></returns>
 6         public static int Min(int[] array)
 7         {
 8             if (array == null) throw new Exception("数组空异常");
 9             int value = 0;
10             bool hasValue = false;
11             foreach (int x in array)
12             {
13                 if (hasValue)
14                 {
15                     if (x < value) value = x;
16                 }
17                 else
18                 {
19                     value = x;
20                     hasValue = true;
21                 }
22             }
23             if (hasValue) return value;
24             throw new Exception("没找到");
25         }

最大值

 1         /// <summary>
 2         /// 最大值
 3         /// </summary>
 4         /// <param name="array"></param>
 5         /// <returns></returns>
 6         public static int Max(int[] array)
 7         {
 8             if (array == null) throw new Exception("数组空异常");
 9             int value = 0;
10             bool hasValue = false;
11             foreach (int x in array)
12             {
13                 if (hasValue)
14                 {
15                     if (x > value)
16                         value = x;
17                 }
18                 else
19                 {
20                     value = x;
21                     hasValue = true;
22                 }
23             }
24             if (hasValue) return value;
25             throw new Exception("没找到");
26         }

平均值

 1         /// <summary>
 2         /// 平均值
 3         /// </summary>
 4         /// <param name="source"></param>
 5         /// <returns></returns>
 6         public static double? Average(int[] array)
 7         {
 8             if (array == null) throw new Exception("数组空异常");
 9             long sum = 0;
10             long count = 0;
11             checked
12             {
13                 foreach (int? v in array)
14                 {
15                     if (v != null)
16                     {
17                         sum += v.GetValueOrDefault();
18                         count++;
19                     }
20                 }
21             }
22             if (count > 0) return (double)sum / count;
23             return null;
24         }

4.测试输出

测试代码

 1         static void Main(string[] args)
 2         {
 3             int[] array = new int[] { 1,2,4,3,0,-1,34,545,2,34};
 4 
 5             Console.WriteLine("--------------Array自身方法-----------------");
 6             Console.WriteLine("Min:{0}",array.Min());
 7             Console.WriteLine("Max:{0}", array.Max());
 8             Console.WriteLine("Average:{0}", array.Average());
 9 
10             Console.WriteLine("---------------内部实现方法------------------");
11             int min = Program.Min(array);
12             int max = Program.Max(array);
13             double? average = Program.Average(array);
14             Console.WriteLine("Min:" + min);
15             Console.WriteLine("Max:" + max);
16             Console.WriteLine("Average:" + average);
17             Console.Read();
18         }

输出结果

以上代码也是从.NET Framework中摘出来的,实际上 Array的自带求最大值、最小值、平均值的算法就是这样做的,在.NET Framework源码中可以看到

5.工程源码下载

源代码下载

 

posted @ 2018-09-05 13:04  JiYF  阅读(51678)  评论(0编辑  收藏  举报