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

转载自:http://www.cnblogs.com/dooroo/archive/2012/12/11/2813858.html

先建一个学生类:

复制代码
        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 += "**原始显示:\r\n";
            showList(Students);

            richTextBox1.Text += "\r\n**用序号排序从小到大显示:\r\n";
            Students.Sort((x, y) => x.Number < y.Number ? -1 : 0);
            showList(Students);

            richTextBox1.Text += "\r\n**用序号排序从大到小显示:\r\n";
            Students.Sort((x, y) => x.Number > y.Number ? -1 : 0);
            showList(Students);

            richTextBox1.Text += "\r\n**用姓名排序(升序)显示:\r\n";
            Students.Sort((x, y) => x.Name.CompareTo(y.Name));
            showList(Students);

            richTextBox1.Text += "\r\n**用姓名排序(降序)显示:\r\n";
            Students.Sort((x, y) => y.Name.CompareTo(x.Name));
            showList(Students);

            richTextBox1.Text += "\r\n**用性别排序(升序)显示:\r\n";
            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() + "\r\n";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "\r\n**找出Name=\"张四\"的学生:\r\n";
            richTextBox1.Text += Students.Find((student s) => s.Name == "张四").ToString();

            richTextBox1.Text += "\r\n\r\n**找出第一个男学生:";
            richTextBox1.Text += "(该方法只会找到第一个就停止)\r\n";
            richTextBox1.Text += Students.Find((student s) => s.Sex == true).ToString();

            richTextBox1.Text += "\r\n\r\n**找出所有女学生:\r\n";
            showList(Students.FindAll((student s) => s.Sex == false));

            richTextBox1.Text += "\r\n\r\n**判断“张四”学生是否存在:\r\n";
            richTextBox1.Text += Students.Exists((student s) => s.Name == "张四" ? true : false).ToString();

        }
    }
}
复制代码

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

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


回答: 如果在使用git拉取项目时出现"fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists."的错误提示,有几种可能的解决方法。首先,确保你有正确的访问权限并且仓库确实存在。你可以尝试使用命令"git clone"来克隆仓库,确保仓库的URL是正确的。如果仓库确实存在并且你有正确的访问权限,那么可能是由于网络问题导致无法连接到远程仓库。你可以尝试检查你的网络连接,或者尝试使用其他网络环境。另外,你还可以尝试使用SSH协议来进行克隆,这通常可以解决一些访问权限的问题。如果以上方法都没有解决问题,你可以尝试参考其他开发者遇到类似问题的解决方案,比如在\[2\]中提到的命令。 #### 引用[.reference_title] - *1* [git拉取失败,没有权限:Please make sure you have the correct access rights and the repository exist](https://blog.csdn.net/weixin_43928112/article/details/131196951)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [fatal: repository does not exist(亲测有效)](https://blog.csdn.net/qq_58035032/article/details/129145037)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Please make sure you have the correct access rights and the repository exist](https://blog.csdn.net/qq_40468795/article/details/81430658)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值