List<>中Find的用法小结

时间:2022-05-25 18:41:44 

I've been looking for help on how to find objects in Generics with List.Find() method .... and ... take a look what I have found.
In the follow example, I created a simple class:


public class Person
{
       private int _id;
       private string _name;

       public int ID {  get{ return _id;} set{ _id = value;}}
       public int Name {  get{ return _name;} set{ _name= value;}}

       public Person(int id, string name)
       {
             _id = id;
             _name = name;
       }
}

In the example, there's a simple class with two private attributes. Now we're going to create a typed List of this object and take advantage of the Find() method


public void CreateAndSearchList()
{
      //create and fill the collection
      List<Person> myList = new List<Person>();
      myList.Add(new Person(1, "AndreySanches"));
      myList.Add(new Person(2, "AlexandreTarifa"));
      myList.Add(new Person(3, "EmersonFacunte"));

     //find a specific object
     Person myLocatedObject = myList.Find(delegate(Person p) {return p.ID == 1; });
}

备注:在list和array集合中搜索元素经常使用该方法,主要技术是泛型委托

list用法注意:如果增加一个对象,必须重新new一个对象,看下面的例子:


person p=new pewson();
for(int i=0;i<5;i++)
{
p.ID=i;
p.Name="xxxx";
list.add(p);
}

for(int i=0;i<5;i++)
{
person p=new person();
p.ID=i;
p.Name="xxxx";
list.add(p);
}

上面有区别吗?在输出list的值是有区别了,第一个list里面存放的都是一样的,结果和最后一个一样,都是同一个人对象,第二个list达到预期的效果,存储不同的人对象。这是为什么?原因很简单,一个list对象中存储的都是对一个共有的对象进行引用,所以以最后改变的值为准。

对象的排序:本文主要阐述对存储datatable的list进行按TableName排序

1、新建一个sort类


public class SceneSort:IComparer<DataTable>
    {
       public int Compare(DataTable obj1, DataTable obj2)
       {
           int tableNameLength1;
           int tableNameLength2;
           if (obj1 == null)
           {
               if (obj2 == null)
                   return 0;
               else
                   return -1;
           }
           else
           {
               if (obj2 == null)
                   return 1;
               else
               {
                   tableNameLength1=obj1.TableName.Length;
                   tableNameLength2=obj2.TableName.Length;
                   if (Convert.ToInt32(obj1.TableName.Substring(2,tableNameLength1-2))>Convert.ToInt32(obj2.TableName.Substring(2,tableNameLength2-2)))
                       return 1;                            //大于返回1
                   else if (Convert.ToInt32(obj1.TableName.Substring(2,tableNameLength1-2))<Convert.ToInt32(obj2.TableName.Substring(2,tableNameLength2-2)))
                       return -1;                           //小于返回-1
                   else
                       return 0;                            //相等返回0
               }
           }

       }

2 排序:
List<DataTable> ldt = new List<DataTable>();
SceneSort ss=new SceneSort ();
tablelist.sort(ss);即可对list进行排序;

标签:Find用法,List
0
投稿

猜你喜欢

  • 详解springcloud Feign的Hystrix支持

    2022-07-08 20:15:13
  • 全面解析java final关键字

    2023-03-16 03:04:55
  • Android自定义View实现课程表表格

    2023-03-11 19:33:08
  • android开发教程之判断是手机还是平板的方法

    2022-10-22 12:30:41
  • 深入解析Jdk8中Stream流的使用让你脱离for循环

    2023-03-01 21:50:43
  • WinForm实现状态栏跑马灯效果的方法示例

    2023-07-16 19:37:33
  • Spring Boot集成ElasticSearch实现搜索引擎的示例

    2021-06-02 05:06:16
  • Springcloud seata nacos环境搭建过程图解

    2022-11-15 00:34:14
  • Java中的zookeeper常用命令详解

    2022-12-07 05:46:35
  • Android SharedPreferences存储用法详解

    2023-08-07 08:25:34
  • C#获取本地IP的四种方式示例详解

    2023-04-16 00:52:50
  • 一文带你搞懂Java8的LocalDateTime

    2023-11-10 02:02:22
  • Java进程cpu占用过高问题解决

    2021-08-09 00:16:59
  • 基于@Bean修饰的方法参数的注入方式

    2022-09-16 06:45:14
  • springboot 返回json格式数据时间格式配置方式

    2023-12-17 23:27:10
  • Http学习之组装报文

    2021-12-30 07:23:18
  • WinForm实现移除控件某个事件的方法

    2021-09-25 02:57:58
  • Linux系统中C语言编程创建函数fork()执行解析

    2023-06-21 01:10:03
  • IDEA中java断言assert语法及使用

    2022-12-28 21:07:00
  • C#中的TemplateMethod模式问题分析

    2021-12-01 04:02:44
  • asp之家 软件编程 m.aspxhome.com