C#求數組最大值或最大值位置索引

常見求最大值,是數值型數組,這個通常遍歷數組方式,或數組排序即可完成。但對於字符串或日期等非數值類型不能處理。下面給出泛型數組的最大值或最大值位置索引的自定義函數。

數組最大值的位置索引

//傳入一個數組,求出一個數組的最大值的位置
public static int MaxIndex<T>(T[] arr) where T : IComparable<T>
{
    var i_Pos = 0;
    var value = arr[0];
    for (var i = 1; i < arr.Length; ++i)
    {
        var _value = arr[i];
        if (_value.CompareTo(value) > 0)
        {
            value = _value;
            i_Pos = i;
        }
    }
    return i_Pos;
}

數組最大值

//傳入一個數組,求出一個數組的最大值
public static T MaxValue<T>(T[] arr) where T : IComparable<T>
{
    var i_Pos = 0;
    var value = arr[0];
    for (var i = 1; i < arr.Length; ++i)
    {
        var _value = arr[i];
        if (_value.CompareTo(value) > 0)
        {
            value = _value;
            i_Pos = i;
        }
    }
    return value;
}

測試例如下:

int[] arr = { 2, 31, 148, 754, 9143, 0, 4548149, 645, 2, 54 };
Console.WriteLine("Index={0}, Value={1}, Type={2}",MaxIndex(arr),MaxValue(arr), MaxValue(arr).GetType());
            
DateTime[] days = { DateTime.Parse("2019-10-11"), DateTime.Parse("2010-10-11"), DateTime.Parse("2019-10-11 00:00:00") };//, DateTime.Parse("2020-01-11") };
Console.WriteLine("Index={0}, Value={1}, Type={2}", MaxIndex(days), MaxValue(days), MaxValue(days).GetType());

string[] name = { "asdfg", "asdasda", "asda", "wa", "z" };
Console.WriteLine("Index={0}, Value={1}, Type={2}", MaxIndex(name), MaxValue(name), MaxValue(name).GetType());

同樣地,你也可以寫出一個求最小值的函數。

完整VS示例如下:

using System;

namespace ConsoleApp_Test
{
    class Program
    {
        //傳入一個數組,求出一個數組的最大值的位置
        public static int MaxIndex<T>(T[] arr) where T : IComparable<T>
        {
            var i_Pos = 0;
            var value = arr[0];
            for (var i = 1; i < arr.Length; ++i)
            {
                var _value = arr[i];
                if (_value.CompareTo(value) > 0)
                {
                    value = _value;
                    i_Pos = i;
                }
            }
            return i_Pos;
        }
        
        //傳入一個數組,求出一個數組的最大值
        public static T MaxValue<T>(T[] arr) where T : IComparable<T>
        {
            var i_Pos = 0;
            var value = arr[0];
            for (var i = 1; i < arr.Length; ++i)
            {
                var _value = arr[i];
                if (_value.CompareTo(value) > 0)
                {
                    value = _value;
                    i_Pos = i;
                }
            }
            return value;
        }

        static void Main(string[] args)
        {
            // 定義一個數組
            int[] arr = { 2, 31, 148, 754, 9143, 0, 4548149, 645, 2, 54 };
            Console.WriteLine("Index={0}, Value={1}, Type={2}",MaxIndex(arr),MaxValue(arr), MaxValue(arr).GetType());
            
            DateTime[] days = { DateTime.Parse("2019-10-11"), DateTime.Parse("2010-10-11"), DateTime.Parse("2019-10-11 00:00:00") };//, DateTime.Parse("2020-01-11") };
            Console.WriteLine("Index={0}, Value={1}, Type={2}", MaxIndex(days), MaxValue(days), MaxValue(days).GetType());

            string[] name = { "asdfg", "asdasda", "asda", "wa", "z" };
            Console.WriteLine("Index={0}, Value={1}, Type={2}", MaxIndex(name), MaxValue(name), MaxValue(name).GetType());

            Console.ReadKey();
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章