List的几个方法

List=>
List.Find()
List.FindAll()
List.Contains()
List.ForEach()
List.ConvertAll()

1. 先比较Find()跟FindAll()。 这个两个函数都是 遍历List的集合,只是 区别在于FindAll()返回的必须是一个List集合,而Find()可以返回字符串。

            List<string> list = new List<string>();
list.Add("小三005");
list.Add("小四007");
list.Add("小五0105");
list.Add("小六007"); string names = list.Find(t => t.ToString().Substring(t.Length - , ) == "");
Console.WriteLine("names:{0}", names); List<string> sList = list.FindAll(t => t.ToString().Substring(t.Length - , ) == "");
foreach (string item in sList)
{
Console.WriteLine("sList:{0}", item);
}
        Console.ReadLine();

2. List.Contains()  。Contains() 函数是查看List集合中是否存在某一值,返回的是 bool 值

            List<string> list = new List<string>();

            list.Add("小三005");
list.Add("小四007");
list.Add("小五0105");
list.Add("小六007"); if (list.Contains("小五0105"))
{
Console.WriteLine("list中存在=>小五0105");
}
else
{
Console.WriteLine("list中不存在=>小五0105");
}

3. List.ForEach() 。ForEach() 也是遍历List 集合,只是它没有返回值,可以跟普通语法的foreach() 一样。

            List<string> list = new List<string>();

            list.Add("小三005");
list.Add("小四007");
list.Add("小五0105");
list.Add("小六007"); list.ForEach(t => Console.WriteLine("list输出{0}", t.ToString()));

4.List.ConvertAll()。

           List<string> list = new List<string>();

            list.Add("小三005");
list.Add("小四007");
list.Add("小五0105");
list.Add("小六007"); List<string> cList = list.ConvertAll<string>( m=> m.ToString());

随机推荐

  1. laravel中有条件使用where

    在项目开发的过程中;有时候会有多个参数 去用在where查询中;那么这里的where语句是可能有也可能没有的 1.用原生的mysql语句来实现 private function getData($ty ...

  2. How to get the MD5 checksum for a file: md5sum, digest, csum, fciv

    LINUX: md5sum fileName In Linux, the md5sum utility can be used: aemtux1:/ % md5sum binary.file 0c46 ...

  3. thinkphp遇到的小问题,js文件中U方法不被解析

    我想在js文件中写ajax, 写完发现异常, 本以为是js文件中不支持ajax 后来发现时地址解析错误. 也就是U方法在js文件中不被解析. 貌似thinkphp解析,tpl文件中的一些元素. js文 ...

  4. js 格式化相关的时间

    javascript Date format(js日期格式化) 方法一: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s). ...

  5. Docker - 使用 swarm 部署 services

    前言 经过之前一段时间学习与思考,我们已经大概明确了一些感念: Docker image/container,  service and node 简单来说,swarm允许我们以节点(node)的方式 ...

  6. 第三章 服务治理: Spring Cloud Eureka

    Spring Cloud Eureka是 Spring Cloud Netflix微服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的服务治理功能 服务治理 ...

  7. Django的views使用

    这里介绍一下Django中常用的类视图,主要说明在视图中如何接收和传递参数.返回到页面等. 注意,使用这些类视图时,在url中需要加上.as_view(). 我将介绍的内容分为三部分:django的V ...

  8. Android Architecture Components

    https://developer.android.com/topic/libraries/architecture/index.html ViewModel 有LiveData Activity 监 ...

  9. springBean之BeanFactory与ApplicationContext

    一.主要区别: 两者都是通过xml配置文件加载bean,ApplicationContext和BeanFacotry相比,提供了更多的扩展功能,但其主要区别在于后者是延迟加载,如果Bean的某一个属性 ...

  10. 张超超OC基础回顾03_结构体类型作为成员变量的特殊用法

    直接上例子: 要求: 合理的设计一个”学生“类 学生有* 姓名* 生日两个属性和说出自己姓名生日方法  要求利用设计的学生类创建学生对象,并说出自己的姓名和年龄 描述学生类 事物名称: 学生(Stud ...