C# simple operation: common methods of List in C# to determine existence, search, and sort

List is commonly used in projects for data operation and management. There are some methods that are often used in Baidu, so they are recorded here.

Table of contents

1. List determines whether the element exists and returns bool

2. List search, return object

3. List sorting

4. Object attribute printing

5. List other methods

1. List determines whether the element exists and returns bool

 if(personList.Exists(t => t.name == "John")){//lable表达式一下List里面单个value的name属性 是否符合 “XXX”

}

2. List search, return object

//Lable表达式一下Find value里面的name属性  && age属性 是否与判断条件相同 
Person temp = personList.Find(t => t.name == "Jack" && t.age > 30 && t.sex == true); 

3. List sorting

class Person : IComparable<Person>
    {
        public string name;
        public int age;
        public bool sex;
 
       
        //定义比较方法,按照 age 比较
        public int CompareTo(Person other)
        {
 
            if (this.age < other.age)
            {
                return -1;
            }
            return 1;
        }
    }
 
personList.Sort();

4. Object attribute printing

    class Person : IComparable<Person>
    {
        public string name;
        public int age;
        public bool sex;
 
        //打印对象实例
        public override string ToString()
        {
            return "name: " + name + ";age: " + age + ";sex: " + sex;
        }
    }
 
person.ToString();

Full version test code:

namespace CSharpApp
{
    class Person : IComparable<Person>
    {
        public string name;
        public int age;
        public bool sex;
 
        public Person(string Name, int Age, bool Sex)
        {
            this.name = Name;
            this.age = Age;
            this.sex = Sex;
        }
 
 
        //定义比较方法,按照 age 比较
        public int CompareTo(Person other)
        {
 
            if (this.age < other.age)
            {
                return -1;
            }
            return 1;
        }
 
        //打印对象实例的时候使用
        public override string ToString()
        {
            return "name: " + name + ";age: " + age + ";sex: " + sex;
        }
    }
 
    class ListTest
    {
        
        static void Main(string[] args)
        {
            List<Person> personList;
            personList = new List<Person>();
 
            //给List赋值
            Person p1 = new Person("Mike", 30, true);
            Person p2 = new Person("John", 20, false);
            Person p3 = new Person("Jack", 50, true);
            personList.Add(p1);
            personList.Add(p2);
            personList.Add(p3);
 
            //List排序
            personList.Sort();
 
            if (personList.Exists(t => t.name == "John"))
            {
                //如果List中存在 name == John的元素
            }
 
            //查找 name 等于 Jack、年龄大于30、性别男的元素
            Person temp = personList.Find(t => t.name == "Jack" && t.age > 30 && t.sex == true);
            //打印找到的对象实例
            //temp.ToString();
 
            Console.WriteLine(temp.ToString());  //换行输出内容
        }
 
        
    }
}
 

5. List other methods

 
//定义学生类
public class Student
{
    private int id;//id
    private string name;//姓名
    private int student_//学号
    public int ID
    {
        get { return id; }
        set { id = value; }
  
    public string NAME
    {
        get { return name; }
        set { name = value; }
  
    public int STUDENT_ID
    {
        get { return student_id; }
        set { student_id = value; }
    }
}


//赋值
Student student = new Student();
List<Student> list = new List<Student>();
for (int i = 0; i < 20; i++)
{
        student.ID = i;
        student.NAME = "学生" + i;
        student.STUDENT_ID = 2000 + i;
        list.Add(student);
        student = new Student();
}

 


//使用Find: public T Find(Predicate match) { ... }

    Student result1 = list.Find((Student s) => s.NAME == "学生15");
    Console.WriteLine(@"ID:{0};姓名:{1};学号:{2}", result1.ID, 
result1.NAME, result1.STUDENT_ID);
    Console.ReadKey();
    Console.WriteLine();

//结果: ID:15;姓名:学生15;学号:2015


//使用FindAll:public List<T> FindAll(Predicate<T> match)
//查找所有ID大于10的学生
    List<Student> result2 = list.FindAll((s) => { return s.ID > 10; });
    foreach (var item in result2)
    {
        Console.WriteLine(@"ID:{0};姓名:{1};学号:{2}", item.ID, 
item.NAME, item.STUDENT_ID);
    }
    Console.ReadKey();
    Console.WriteLine();
结果:
ID:11;姓名:学生11;学号:2011
ID:12;姓名:学生12;学号:2012
ID:13;姓名:学生13;学号:2013
ID:14;姓名:学生14;学号:2014
ID:15;姓名:学生15;学号:2015
ID:16;姓名:学生16;学号:2016
ID:17;姓名:学生17;学号:2017
ID:18;姓名:学生18;学号:2018
ID:19;姓名:学生19;学号:2019

3、使用FindLast:public T FindLast(Predicate<T> match) 找到最后一个(最新一个)姓名里面有1的

    Student result3 = list.FindLast((Student s) => s.NAME.Contains("1"));
    Console.WriteLine(@"ID:{0};姓名:{1};学号:{2}", result3.ID, 
    result3.NAME, result3.STUDENT_ID);
    Console.ReadKey();
    Console.WriteLine();
 

结果:
ID:19;姓名:学生19;学号:2019

4、使用FindIndex

 public int FindIndex(Predicate<T> match)
找到学生姓名为“学生15”的序号
    int result4 = list.FindIndex((Student s) => s.NAME == "学生15");
    Console.WriteLine(@"序号:{0}", result4);
    Console.ReadKey();
    Console.WriteLine();

 结果:
序号:15

Count - List元素数量
 
Add(T item) - 向List添加对象
AddRange() - 尾部添加实现了 ICollection 接口的多个元素
BinarySearch() - 排序后的List内使用二分查找
Clear() - 移除所有元素
Contains(T item) - 测试元素是否在List内
CopyTo(T[] array) - 把List拷贝到一维数组内
Exists() - 判断存在
Find() - 查找并返回List内的出现的第一个匹配元素
FindAll() - 查找并返回List内的所有匹配元素
GetEnumerator() - 返回一个用于迭代List的枚举器
GetRange() - 拷贝指定范围的元素到新的List内
IndexOf() - 查找并返回每一个匹配元素的索引
Insert() - 在List内插入一个元素
InsertRange() - 在List内插入一组元素
LastIndexOf() - 查找并返回最后一个匹配元素的索引
Remove() - 移除与指定元素匹配的第一个元素
RemoveAt() - 移除指定索引的元素
RemoveRange() - 移除指定范围的元素
Reverse() - 反转List内元素的顺序
Sort() - 对List内的元素进行排序
ToArray() - 把List内的元素拷贝到一个新的数组内
trimToSize() - 将容量设置为List中元素的实际数目


 

Guess you like

Origin blog.csdn.net/weixin_39114763/article/details/127851741