`
sty2008boy
  • 浏览: 299355 次
  • 性别: Icon_minigender_1
  • 来自: 太原
社区版块
存档分类
最新评论

List.FindAll

阅读更多
using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        List<string> dinosaurs = new List<string>();

        dinosaurs.Add("Compsognathus");
        dinosaurs.Add("Amargasaurus");
        dinosaurs.Add("Oviraptor");
        dinosaurs.Add("Velociraptor");
        dinosaurs.Add("Deinonychus");
        dinosaurs.Add("Dilophosaurus");
        dinosaurs.Add("Gallimimus");
        dinosaurs.Add("Triceratops");

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nTrueForAll(EndsWithSaurus): {0}",
            dinosaurs.TrueForAll(EndsWithSaurus));

        Console.WriteLine("\nFind(EndsWithSaurus): {0}", 
            dinosaurs.Find(EndsWithSaurus));

        Console.WriteLine("\nFindLast(EndsWithSaurus): {0}",
            dinosaurs.FindLast(EndsWithSaurus));

        Console.WriteLine("\nFindAll(EndsWithSaurus):");
        List<string> sublist = dinosaurs.FindAll(EndsWithSaurus);

        foreach(string dinosaur in sublist)
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine(
            "\n{0} elements removed by RemoveAll(EndsWithSaurus).", 
            dinosaurs.RemoveAll(EndsWithSaurus));

        Console.WriteLine("\nList now contains:");
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nExists(EndsWithSaurus): {0}", 
            dinosaurs.Exists(EndsWithSaurus));
    }

    // Search predicate returns true if a string ends in "saurus".
    private static bool EndsWithSaurus(String s)
    {
        if ((s.Length > 5) && 
            (s.Substring(s.Length - 6).ToLower() == "saurus"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

 

分享到:
评论

相关推荐

    C# list对象FindAll函数的四种写法

    在本篇文章中,我们将详细探讨`List&lt;T&gt;`的`FindAll`方法,并通过四种不同的写法来演示如何使用这个功能来筛选满足特定条件的元素。`FindAll`方法用于在列表中找到符合指定条件的所有元素,返回一个新的`List&lt;T&gt;`实例...

    c#中list.FindAll与for循环的性能对比总结

    有两种常用的方法:`List&lt;T&gt;.FindAll`方法和使用传统的`for`循环。这两种方式在性能上有何差异呢?这篇总结将深入探讨这个问题,并提供实际的测试结果。 首先,`List&lt;T&gt;.FindAll`方法是一个方便的 LINQ 扩展方法,...

    正则综合处理处理字符串.py

    a = re.findall(r"(.+)/", i) b = re.sub(a[0], '', i) c = b.split('.') haha = '' if '_' in c[0]: if len(c[0].split('_')[1]) == 12: haha = c[0].split('_')[1] if len(c[1]) == 12 and '_' not...

    精华志 C#_List_<T>+Find+Exists+FindAll+Sort用法+范例

    本篇文章将详细讲解`List&lt;T&gt;`的一些关键方法,包括`Find`、`Exists`、`FindAll`以及`Sort`,并结合实际范例进行深入解析。 `List&lt;T&gt;`是泛型类,它继承自`Collection&lt;T&gt;`,实现了`IList&lt;T&gt;`接口,用于存储固定数量的...

    Sprider.py

    root_htmls = re.findall(Sprider.list_pattern,htmls) anchors = [] for html in root_htmls: name = re.findall(Sprider.name_pattern,html) number = re.findall(Sprider.number_pattern,html) anchor = {'...

    groovy-demo.pdf

    def shorts = list.findAll { it.size() ``` 这里的闭包`{ it.size() 用于过滤列表中的元素,其中`it`表示当前迭代的元素。 #### 集合操作 Groovy提供了丰富的集合操作方法,比如`each`、`collect`等,这些方法...

    C# List中FindAll用法的一些简单示例

    本文将详细介绍 `List&lt;T&gt;` 中的 `FindAll` 方法的使用,并通过几个简单的示例进行解释。 `FindAll` 方法是 `List&lt;T&gt;` 类的一个成员,它接受一个谓词(Predicate)作为参数,返回一个新列表,其中包含满足该谓词条件...

    查看进程工具tlist.exe

    To find a process ID, type tlist without additional parameter. Pattern Displays detailed information about all processes whose names or window titles match the specified pattern. Pattern can be ...

    C# 3.0 锐利体验系列课程(2):Lambda表达式.pdf

    list.FindAll(delegate(string s){ return s.IndexOf("abc") &gt; 0; }); // 使用C#3.0 Lambda表达式 list.FindAll(s =&gt; s.IndexOf("abc") &gt; 0); ``` #### Lambda表达式的基本格式 Lambda表达式的通用格式如下所示...

    spring3.0.2api 文档spring表达式语言

    5. **列表和集合操作**:SpEL允许对数组、列表和集合进行操作,如索引访问(`list[0]`)、大小检查(`list.size()`)和元素过滤(`list.findAll { it &gt; 10 }`)。 6. **方法调用和函数**:除了调用对象的方法,SpEL...

    Groovy轻松入门

    list.findAll { it % 2 == 0 }.each { println it } ``` 此外,Groovy的GString(Groovy字符串)允许在字符串中嵌入表达式,提供模板化的字符串生成: ```groovy def name = "Alice" def age = 25 println "My name ...

    Device Console (DevCon) Tool_01_源码修正编译通过,VS2013可调试.rar

    findall Find devices, including those that are not currently attached. help Display Devcon help. hwids List hardware IDs of devices. install Install a device manually. listclass List all devices in a ...

    GENESIS 2000 DFM PE

    Bringing It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Chapter 3 Advanced Topics Implementation of Objects in DFMPE . . . . . . . . . . . . . . . . . . . 34 Layer and ...

    权限树,就是也权限控制

    package com.tansuo.biz; import java.util.List; import com.tansuo.dao.RoleDAO; import com.tansuo.entity.Role; ...public class RoleBiz { ... public List findall(){ return rdao.findAll(); } }

    cnnvd爬虫,仅供学习参考。

    list4.append(str(link.contents[3].contents[3].find('a').text.lstrip().rstrip())) except: #print("危害等级:is empty") list4.append("") try: #print ("CVE编号:"+link.contents[3].contents[5].find('a...

    java学生成绩管理系统

    public List FindAll(int pageNow,int pageSize) { return kcDao.findAll(pageNow, pageSize); } //查询一共多少条课程记录 /* (non-Javadoc) * @see chao.serviceimp.KcService#findKcSize() */ public ...

Global site tag (gtag.js) - Google Analytics