C#-----集合List<T>的常用方法

2021-06-09 23:11

阅读:556

标签:tostring   names   contain   equals   cti   strong   index   back   ica   

    雇员实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestList
{
    /// 
    /// 雇员类
    /// 
    public class Employee
    {
        /// 
        /// 雇员姓名
        /// 
        public string EmpName { get; set; }
        /// 
        /// 雇员性别
        /// 
        public string EmpSex { get; set; }
        /// 
        /// 雇员年龄
        /// 
        public int EmpAge { get; set; }
        /// 
        /// 雇员部门
        /// 
        public string DeptName { get; set; }
        /// 
        /// 构造函数
        /// 
        /// 
        /// 
        /// 
        /// 
        public Employee(string empName, string empSex, int empAge, string deptName)
        {
            EmpName = empName;
            EmpSex = empSex;
            EmpAge = empAge;
            DeptName = deptName;
        }

        public override string ToString()
        {
            return "Employee[EmpName=" + EmpName + ",EmpSex=" + EmpSex + ",EmpAge=" + EmpAge + ",DeptName=" + DeptName + "]";
        }
    }
}

   1.ForEach(Action action)

      对集合的每个元素执行指定操作

List listEmps = new List();
            //ForEach(Action action) 对 System.Collections.Generic.List`1 的每个元素执行指定操作
            employees.ForEach(p =>
            {
                if (p.EmpSex.Equals(""))
                {
                    listEmps.Add(p);
                }
            });

   2.FindAll(Predicate match)

      检索与指定谓词定义的条件匹配的所有元素

 List empList = employees.FindAll(p=>(p.EmpAge>22));
            if (empList.Count>0)
            {
                foreach (Employee emp in empList)
                {
                    Console.WriteLine(emp.ToString());
                }
            }

   3.Where

     基于谓词筛选值序列

List listEmployee = employees.Where(p => (p.EmpName.Contains(""))).ToList();
            if (listEmployee.Count>0)
            {
                foreach (Employee emp in listEmployee)
                {
                    Console.WriteLine(emp.ToString());
                }
            }

   4.RemoveAll(Predicate match) 

     移除与指定的谓词所定义的条件相匹配的所有元素

employees.RemoveAll(p => (p.EmpAge >= 25));
            if (employees.Count>0)
            {
                foreach (Employee emp in employees)
                {
                    Console.WriteLine(emp.ToString());
                }
            }

   5.RemoveAt(int index) 

     移除指定索引处的元素

if (employees.Count>0)
            {
                for (int i=0;i)
                {
                    Employee employee = employees[i];
                    if (employee.DeptName.Equals("市场部"))
                    {
                        employees.RemoveAt(i);
                    }
                }
                if (employees.Count > 0)
                {
                    foreach (Employee emp in employees)
                    {
                        Console.WriteLine(emp.ToString());
                    }
                }
            }

     完整Demo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestList
{
    class Program
    {
        static void Main(string[] args)
        {
            List employees = new List();
            Employee empOne = new Employee("王晶", "", 20, "市场部");
            Employee empTwo = new Employee("陈浩民", "", 24, "技术部");
            Employee empThree = new Employee("王诗玲", "", 25, "市场部");
            Employee empFour = new Employee("陈绍聪", "", 30, "技术部");
            Employee empFive = new Employee("张倩", "", 19, "行政部");
            employees.Add(empOne);
            employees.Add(empTwo);
            employees.Add(empThree);
            employees.Add(empFour);
            employees.Add(empFive);

            List listEmps = new List();
            //ForEach(Action action) 对 System.Collections.Generic.List`1 的每个元素执行指定操作
            employees.ForEach(p =>
            {
                if (p.EmpSex.Equals(""))
                {
                    listEmps.Add(p);
                }
            });
            if (listEmps.Count>0)
            {
                foreach (Employee emp in listEmps)
                {
                    Console.WriteLine(emp.ToString());
                }
            }
            Console.WriteLine("=========================");

            //Where 基于谓词筛选值序列
            List listEmployee = employees.Where(p => (p.EmpName.Contains(""))).ToList();
            if (listEmployee.Count>0)
            {
                foreach (Employee emp in listEmployee)
                {
                    Console.WriteLine(emp.ToString());
                }
            }
            Console.WriteLine("=========================");

            //FindAll(Predicate match) 检索与指定谓词定义的条件匹配的所有元素
            List empList = employees.FindAll(p=>(p.EmpAge>22));
            if (empList.Count>0)
            {
                foreach (Employee emp in empList)
                {
                    Console.WriteLine(emp.ToString());
                }
            }
            Console.WriteLine("=========================");

            //RemoveAll(Predicate match) 移除与指定的谓词所定义的条件相匹配的所有元素
            employees.RemoveAll(p => (p.EmpAge >= 25));
            if (employees.Count>0)
            {
                foreach (Employee emp in employees)
                {
                    Console.WriteLine(emp.ToString());
                }
            }
            Console.WriteLine("=========================");

            //RemoveAt(int index) 移除指定索引处的元素
            if (employees.Count>0)
            {
                for (int i=0;i)
                {
                    Employee employee = employees[i];
                    if (employee.DeptName.Equals("市场部"))
                    {
                        employees.RemoveAt(i);
                    }
                }
                if (employees.Count > 0)
                {
                    foreach (Employee emp in employees)
                    {
                        Console.WriteLine(emp.ToString());
                    }
                }
            }

            Console.ReadLine();
        }
    }
}

 

C#-----集合List的常用方法

标签:tostring   names   contain   equals   cti   strong   index   back   ica   

原文地址:https://www.cnblogs.com/fengfuwanliu/p/10577311.html

上一篇:WPF上下滚动字幕

下一篇:c# json


评论


亲,登录后才可以留言!