zoukankan      html  css  js  c++  java
  • [C#]List的Sort()、Find()、FindAll()、Exist()的使用方法举例

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

    List函数用得还是比较多的,正好用到其中的向个方法,做了一个例程,再总结一下:

    先建一个学生类:

    复制代码
            public class student
            {
                public int Number { get; set; }
                public string Name { get; set; }
                public bool Sex { get; set; }
                public student(int _number, string _name, bool _sex)
                {
                    Number = _number;
                    Name = _name;
                    Sex = _sex;
                }
                public override string ToString()
                {
                    return string.Format("序号:{0},姓名:{1},性别:{2}",
                        Number.ToString(), Name, Sex ? "" : "");
                }
            }
    复制代码

    例程代码如下:

    复制代码
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ListSortTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            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 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 showList(List<student> _list)
            {
                for (int i = 0; i < _list.Count; i++)
                {
                    richTextBox1.Text += _list[i].ToString() + "
    ";
                }
            }
    
            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();
    
            }
        }
    }
    复制代码

    通过以上代码测试,排序效果如下:

    其它功能显示如图(欢迎访问http://www.cnblogs.com/dooroo)

  • 相关阅读:
    学号160809212姓名田京诚C语言程序设计实验2选择结构程序设计
    自我介绍
    Python中如何实现im2col和col2im函数(sliding类型)
    二维DCT变换
    android 进阶之android中隐藏的layout (抽屉)
    如何申请个人Google API Key用于Android真机上发布安装
    Android:在不同的Activity之间进行数据的共享
    Androi重写EditText改变边框
    activity添加ScrollView后onFling不起作用,无法滑动问题
    根据名称获去资源ID (反射)
  • 原文地址:https://www.cnblogs.com/opop/p/5276411.html
Copyright © 2011-2022 走看看