• [C#]List的Sort()、Find()、FindAll()、Exist()的使用方法举例


    [转]http://www.cnblogs.com/dooroo/archive/2012/12/11/2813858.html

     public class student
        {
            //构造函数
            public student(int _number, string _name, bool _sex)
            {
                Number = _number;
                Name = _name;
                Sex = _sex;
            }
            public int Number { get; set; }
            public string Name { get; set; }
            public bool Sex { get; set; }
    
            public override string ToString()
            {
                return string.Format("序号:{0},姓名:{1},性别:{2}", Number.ToString(), Name, Sex ? "" : "");
            }
    
        }
     List<student> Students = new List<student>();
            private void Form1_Load(object sender,EventArgs e)
            {
                Students.Add(new student(1, "张一", true));
                Students.Add(new student(3, "张二", false));
                Students.Add(new student(5, "张三", true));
                Students.Add(new student(2, "张四", false));
                Students.Add(new student(4, "张五", true));
                Students.Add(new student(6, "张六", false));
            }
    
           
            private void showList(List<student> _list)
            {
                foreach(var item in _list)
                {
                    richTextBox1.Text += item.ToString() + "
    ";
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                richTextBox1.Text += "**原始显示:
    ";
                showList(Students);
    
                richTextBox1.Text += "
    **用序号排序从小到大显示:
    ";
                Students.Sort((x, y) => x.Number < y.Number ? -1 : 0);
                showList(Students);
    
                richTextBox1.Text += "
    **用序号排序从大到小显示:
    ";
                Students.Sort((x, y) => x.Number > y.Number ? -1 : 0);
                showList(Students);
    
                richTextBox1.Text += "
    **用姓名排序(升序)显示:
    ";
                Students.Sort((x, y) => x.Name.CompareTo(y.Name));
                showList(Students);
    
                richTextBox1.Text += "
    **用姓名排序(降序)显示:
    ";
                Students.Sort((x, y) => y.Name.CompareTo(x.Name));
                showList(Students);
    
                richTextBox1.Text += "
    **用性别排序(升序)显示:
    ";
                Students.Sort((x, y) => x.Sex.CompareTo(y.Sex));
                showList(Students);
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                richTextBox1.Text += "
    **找出Name="张四"的学生:
    ";
                richTextBox1.Text += Students.Find((student s) => s.Name == "张四").ToString();
    
                richTextBox1.Text += "
    
    **找出第一个男学生:";
                richTextBox1.Text += "(该方法只会找到第一个就停止)
    ";
                richTextBox1.Text += Students.Find((student s) => s.Sex == true).ToString();
    
                richTextBox1.Text += "
    
    **找出所有女学生:
    ";
                showList(Students.FindAll((student s) => s.Sex == false));
    
                richTextBox1.Text += "
    
    **判断“张四”学生是否存在:
    ";
                richTextBox1.Text += Students.Exists((student s) => s.Name == "张四" ? true : false).ToString();
            }
  • 相关阅读:
    C# 从类库中获取资源图片,把图片资源保存到类库中
    C# Bitmap/png转成jpg格式,压缩图片
    C# 窗体间传值(使用委托与自定义事件)
    C# Image与Base64编码互转函数
    简单Tomcat HTTP RPC框架
    Java 网络IO编程总结(BIO、NIO、AIO均含完整实例代码)
    Java序列化机制和原理
    攻破JAVA NIO技术壁垒
    代理模式(Proxy)
    Java序列化与反序列化
  • 原文地址:https://www.cnblogs.com/firstcsharp/p/5156665.html
Copyright © 2020-2023  润新知