【转载】ArrayList使用LastIndexOf方法查找最后一个符合条件的元素位置

在C#的编程开发中,ArrayList集合是一个常用的非泛型类集合,在ArrayList集合中如果需要查找最后一个符合条件的元素所在的位置,可以使用ArrayList集合的LastIndexOf方法,LastIndexOf方法将会返回符合条件的最后一个元素在集合中的索引位置信息,如果未查到符合条件的元素对象,则返回-1。

LastIndexOf方法的签名为:virtual int LastIndexOf(object value),参数value代表被查询的值。

例如,有个ArrayList集合存储的数据都为Int类型,集合中含有7个元素,依次为1,10,2,3,4,10,5

   ArrayList arrayList1 = new ArrayList();
   arrayList1.Add(1);
   arrayList1.Add(10);
   arrayList1.Add(2);
   arrayList1.Add(3);
   arrayList1.Add(4);
   arrayList1.Add(10);
   arrayList1.Add(5);

   var lastIndex= arrayList1.LastIndexOf(10);//返回lastIndex=5

 

posted @ 2019-07-27 15:10  江湖逍遥  阅读(2037)  评论(0编辑  收藏  举报