C#-----集合ListT的常用方法
生活随笔
收集整理的這篇文章主要介紹了
C#-----集合ListT的常用方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? 雇員實體類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace TestList {/// <summary>/// 雇員類/// </summary>public class Employee{/// <summary>/// 雇員姓名/// </summary>public string EmpName { get; set; }/// <summary>/// 雇員性別/// </summary>public string EmpSex { get; set; }/// <summary>/// 雇員年齡/// </summary>public int EmpAge { get; set; }/// <summary>/// 雇員部門/// </summary>public string DeptName { get; set; }/// <summary>/// 構造函數/// </summary>/// <param name="empName"></param>/// <param name="empSex"></param>/// <param name="empAge"></param>/// <param name="deptName"></param>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<T> action)
? ? ??對集合的每個元素執行指定操作
List<Employee> listEmps = new List<Employee>();//ForEach(Action<T> action) 對 System.Collections.Generic.List`1 的每個元素執行指定操作employees.ForEach(p =>{if (p.EmpSex.Equals("女")){listEmps.Add(p);}});? ?2.FindAll(Predicate<T> match)
? ? ? 檢索與指定謂詞定義的條件匹配的所有元素
List<Employee> empList = employees.FindAll(p=>(p.EmpAge>22));if (empList.Count>0){foreach (Employee emp in empList){Console.WriteLine(emp.ToString());}}? ?3.Where
? ? ?基于謂詞篩選值序列
List<Employee> listEmployee = employees.Where(p => (p.EmpName.Contains("王"))).ToList();if (listEmployee.Count>0){foreach (Employee emp in listEmployee){Console.WriteLine(emp.ToString());}}? ?4.RemoveAll(Predicate<T> 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<employees.Count;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<Employee> employees = new List<Employee>();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<Employee> listEmps = new List<Employee>();//ForEach(Action<T> 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<Employee> 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<T> match) 檢索與指定謂詞定義的條件匹配的所有元素List<Employee> empList = employees.FindAll(p=>(p.EmpAge>22));if (empList.Count>0){foreach (Employee emp in empList){Console.WriteLine(emp.ToString());}}Console.WriteLine("=========================");//RemoveAll(Predicate<T> 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<employees.Count;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();}} }?
轉載于:https://www.cnblogs.com/fengfuwanliu/p/10577311.html
總結
以上是生活随笔為你收集整理的C#-----集合ListT的常用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构教程 李春葆主编 (第5版)绪
- 下一篇: openbravo erp介绍(一)