欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

List<>中Find的用法小结

程序员文章站 2024-02-25 21:09:51
i've been looking for help on how to find objects in generics with list.find() method...

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进行排序;