C#-----集合List<T>的常用方法
标签: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
评论