通过泛型查找不同数组中的值

来源:互联网 发布:js 指定日期的时间戳 编辑:程序博客网 时间:2024/06/04 18:23
    public partial class ArrayInfo : Form    {        Finder finder = new Finder();        public ArrayInfo()        {            InitializeComponent();        }            String[] str = new string[] { "110", "120", "119" };         class Finder        {            public  int Find<T>(T[] items,T item) //定义一个泛型方法,用来查找指定值在数组中的索引            {                for (int i = 0; i < items.Length; i++) //遍历泛型数组                {                    if(items[i].Equals(item)) //判断是否找到了指定值                    {                        return i; //返回指定值在数组中的索引                    }                }                return -1; //如果没有找到,返回-1            }        }        private void button2_Click(object sender, EventArgs e)        {            string itme = "110";           MessageBox.Show(finder.Find(str,itme).ToString());        }        private void button3_Click(object sender, EventArgs e)        {            int[] i = new int[] { 1, 2, 3, 4, 5, 6, 7 };            MessageBox.Show(finder.Find(i, 5).ToString());        }        private void button1_Click(object sender, EventArgs e)        {            bool[] b = new bool[] { true, false };            MessageBox.Show(finder.Find(b, true).ToString());        }    }

0 0
原创粉丝点击