C#简单操作:C#中List常用方法 判断存在、查找、排序

目常用List来进行数据操作管理,有一些方法经常百度,所以这里记录下。

目录

1. List判断元素是否存在,返回bool

2. List查找,返回对象

3. List排序

4. 对象属性打印

5. List 其他方法

1. List判断元素是否存在,返回bool

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

}

2. List查找,返回对象

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

3. List排序

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. 对象属性打印

    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();

完整版测试代码:

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 其他方法

 
//定义学生类
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中元素的实际数目


 

猜你喜欢

转载自blog.csdn.net/weixin_39114763/article/details/127851741