C#(五十八)之C#List

news2024/11/14 13:58:06

前几天,看同事写的代码中有list相关的字眼,百度了一下,原来是C#中list泛型集合。

了解一下。

List:泛型集合,List<T>类是 ArrayList 类的泛型等效类。该类使用大小可按需动态增加的数组实现 IList<T> 泛型接口。 

泛型的好处: 它为使用c#语言编写面向对象程序增加了极大的效力和灵活性。不会强行对值类型进行装箱和拆箱,或对引用类型进行向下强制类型转换,所以性能得到提高。 

使用命名空间:

<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#f92672">using</span> <span style="color:#f92672">System</span>.Collections.Generic;</span></span>

list一些方法和属性:

1.      Capacity 用于获取或设置List可容纳元素的数量。当数量超过容量时,这个值会自动增长。您可以设置这个值以减少容量,也可以调用trin()方法来减少容量以适合实际的元素数目。

2.      Count 属性,用于获取数组中当前元素数量

3.      Item( ) 通过指定索引获取或设置元素。对于List类来说,它是一个索引器。

4.      Add( ) 在List中添加一个对象的公有方法

5.      AddRange( ) 公有方法,在List尾部添加实现了ICollection接口的多个元素

6.      BinarySearch( ) 重载的公有方法,用于在排序的List内使用二分查找来定位指定元素.

7.      Clear( ) 在List内移除所有元素

8.      Contains( ) 测试一个元素是否在List内

9.      CopyTo( ) 重载的公有方法,把一个List拷贝到一维数组内

10.    Exists( ) 测试一个元素是否在List内

11.    Find( ) 查找并返回List内的出现的第一个匹配元素

12.    FindAll( ) 查找并返回List内的所有匹配元素

13.    GetEnumerator( ) 重载的公有方法,返回一个用于迭代List的枚举器

14.    Getrange( ) 拷贝指定范围的元素到新的List内

15.    IndexOf( ) 重载的公有方法,查找并返回每一个匹配元素的索引

16.    Insert( ) 在List内插入一个元素

17.    InsertRange( ) 在List内插入一组元素

18.    LastIndexOf( ) 重载的公有方法,,查找并返回最后一个匹配元素的索引

19.    Remove( ) 移除与指定元素匹配的第一个元素

20.    RemoveAt( ) 移除指定索引的元素

21.    RemoveRange( ) 移除指定范围的元素

22.    Reverse( ) 反转List内元素的顺序

23.    Sort( ) 对List内的元素进行排序

24.    ToArray( ) 把List内的元素拷贝到一个新的数组内

25.    trimToSize( ) 将容量设置为List中元素的实际数目

 

1:声明list

①、List<T> mList = new List<T>();  
T为列表中元素类型,现在以string类型作为例子

<span style="background-color:#282c34"><span style="color:#abb2bf">List<<span style="color:#e6c07b">string</span>> mList = <span style="color:#f92672">new</span> List<<span style="color:#e6c07b">string</span>>();</span></span>

②、List<T> testList =new List<T> (IEnumerable<T> collection);

以一个集合作为参数创建List:

<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#e6c07b">string</span>[] arr = { <span style="color:#98c379">"Ha"</span>, <span style="color:#98c379">"Hunter"</span>, <span style="color:#98c379">"Tom"</span>, <span style="color:#98c379">"Lily"</span>, <span style="color:#98c379">"Jay"</span>, <span style="color:#98c379">"Jim"</span>, <span style="color:#98c379">"Kuku"</span>, <span style="color:#98c379">"Locu"</span> };
List<<span style="color:#e6c07b">string</span>> mList = <span style="color:#f92672">new</span> List<<span style="color:#e6c07b">string</span>>(arr);</span></span>

2:添加元素 Add

添加一个元素

语法: List. Add(T item)  

<span style="background-color:#282c34"><span style="color:#abb2bf">mList.<span style="color:#e2b93d">Add(<span style="color:#9ccc65">"cmailla"</span>)</span>;
foreach (<span style="color:#e6c07b">string</span> item <span style="color:#f92672">in</span> mList)
{
      Console.<span style="color:#e2b93d">WriteLine(item)</span>;
}</span></span>

3:指定位置添加元素 Insert

语法: Insert(int index, T item); 

<span style="background-color:#282c34"><span style="color:#abb2bf">            mList.Insert(<span style="color:#d19a66">0</span>,<span style="color:#98c379">"qwert"</span>);
            foreach (<span style="color:#e6c07b">string</span> <span style="color:#e6c07b">item</span> <span style="color:#f92672">in</span> mList)
            {
                Console.WriteLine(<span style="color:#e6c07b">item</span>);
            }
            Console.WriteLine(<span style="color:#98c379">"
"</span>);</span></span>

4:添加元素集合 AddRange

语法: List. AddRange(IEnumerable<T> collection)   

<span style="background-color:#282c34"><span style="color:#abb2bf">            <span style="color:#e6c07b">string</span>[] dfg = { <span style="color:#98c379">"hiocvhi"</span>,<span style="color:#98c379">"jioosdfhio"</span>,<span style="color:#98c379">"erw"</span>};
            mList.AddRange(dfg);
            foreach (<span style="color:#e6c07b">string</span> <span style="color:#e6c07b">item</span> <span style="color:#f92672">in</span> mList)
            {
                Console.WriteLine(<span style="color:#e6c07b">item</span>);
            }
            Console.WriteLine(<span style="color:#98c379">"
"</span>);</span></span>

5:删除指定元素

语法:List. Remove(T item)

<span style="background-color:#282c34"><span style="color:#abb2bf">            mList.Remove(<span style="color:#98c379">"erw"</span>);
            foreach (<span style="color:#e6c07b">string</span> <span style="color:#e6c07b">item</span> <span style="color:#f92672">in</span> mList)
            {
                Console.WriteLine(<span style="color:#e6c07b">item</span>);
            }
            Console.WriteLine(<span style="color:#98c379">"
"</span>);</span></span>

6:删除指定下标元素

语法:List. RemoveAt(int index);   

<span style="background-color:#282c34"><span style="color:#abb2bf">            mList.RemoveAt(<span style="color:#d19a66">3</span>);
            foreach (<span style="color:#e6c07b">string</span> <span style="color:#e6c07b">item</span> <span style="color:#f92672">in</span> mList)
            {
                Console.WriteLine(<span style="color:#e6c07b">item</span>);
            }
            Console.WriteLine(<span style="color:#98c379">"
"</span>);</span></span>

7:从下标index开始,删除count个元素

语法:List. RemoveRange(int index, int count);

<span style="background-color:#282c34"><span style="color:#abb2bf">            mList.RemoveRange(<span style="color:#d19a66">3</span>,<span style="color:#d19a66">5</span>);
            foreach (<span style="color:#e6c07b">string</span> <span style="color:#e6c07b">item</span> <span style="color:#f92672">in</span> mList)
            {
                Console.WriteLine(<span style="color:#e6c07b">item</span>);
            }
            Console.WriteLine(<span style="color:#98c379">"
"</span>);</span></span>

8:判断某个元素是否在该List中:

语法:List. Contains(T item)   返回值为:true/false

<span style="background-color:#282c34"><span style="color:#abb2bf">            bool s = mList.Contains(<span style="color:#98c379">"Ha"</span>);
            Console.WriteLine(s);
            Console.WriteLine(<span style="color:#98c379">"</span>
<span style="color:#98c379">");</span></span></span>

9:给List里面元素排序:

语法: List. Sort ()   默认是元素第一个字母按升序

<span style="background-color:#282c34"><span style="color:#abb2bf">            mList<span style="color:#d19a66">.Sort</span>();
            foreach (var item in mList)
            {
                Console<span style="color:#d19a66">.WriteLine</span>(item);
            }
            Console<span style="color:#d19a66">.WriteLine</span>("
");</span></span>

10:给List里面元素顺序反转:

语法: List. Reverse ()   可以与List. Sort ()配合使用,达到想要的效果

<span style="background-color:#282c34"><span style="color:#abb2bf">            mList<span style="color:#d19a66">.Reverse</span>();
            foreach (var item in mList)
            {
                Console<span style="color:#d19a66">.WriteLine</span>(item);
            }
            Console<span style="color:#d19a66">.WriteLine</span>("
");</span></span>

11:List清空:

语法:List. Clear () 

<span style="background-color:#282c34"><span style="color:#abb2bf">            mList<span style="color:#d19a66">.Clear</span>();
            foreach (var item in mList)
            {
                Console<span style="color:#d19a66">.WriteLine</span>(item);
            }
            Console<span style="color:#d19a66">.WriteLine</span>("
");</span></span>

12:ist.FindAll方法:检索与指定谓词所定义的条件相匹配的所有元素 

语法:public List<T> FindAll(Predicate<T> match);

Predicate是对方法的委托,如果传递给它的对象与委托中定义的条件匹配,则该方法返回 true。当前 List 的元素被逐个传递给Predicate委托,并在 List 中向前移动,从第一个元素开始,到最后一个元素结束。当找到匹配项时处理即停止。

Predicate 可以委托给一个函数或者一个拉姆达表达式:

正常委托:

<span style="background-color:#282c34"><span style="color:#abb2bf">        List<<span style="color:#e6c07b">string</span>> subList = mList.FindAll(ListFind); <span style="color:#b18eb1"><em>//委托给ListFind函数</em></span>
        <span style="color:#b18eb1"><em>//ListFind函数</em></span>
        <span style="color:#61aeee"><span style="color:#f92672">public</span> <span style="color:#f92672">static</span> <span style="color:#e6c07b">bool</span> <span style="color:#61aeee">ListFind</span>(<span style="color:#a6e22e"><span style="color:#e6c07b">string</span> name</span>)</span>
        {
            Console.WriteLine(name);
            <span style="color:#f92672">if</span> (name.Length > <span style="color:#d19a66">3</span>)
            {
                <span style="color:#f92672">return</span> <span style="color:#56b6c2">true</span>;
            }
            <span style="color:#f92672">return</span> <span style="color:#56b6c2">false</span>;
        }</span></span>

匿名委托:

<span style="background-color:#282c34"><span style="color:#abb2bf">List<<span style="color:#e6c07b">string</span>> subList = mList.<span style="color:#e2b93d">FindAll( delegate(string name)</span> {
     Console.<span style="color:#e2b93d">WriteLine(name)</span>;
     <span style="color:#f92672">if</span> (name.Length > <span style="color:#d19a66">3</span>)
     {
           return <span style="color:#56b6c2">true</span>;
      }
       return <span style="color:#56b6c2">false</span>;
});</span></span>

Lambda表达式:

<span style="background-color:#282c34"><span style="color:#abb2bf">List<string> subList = mList.FindAll(<span style="color:#61aeee"><span style="color:#a6e22e">name</span> =></span> {
      <span style="color:#e6c07b">Console</span>.WriteLine(name);
      <span style="color:#f92672">if</span> (name.<span style="color:#e6c07b">Length</span> > <span style="color:#d19a66">3</span>)
      {
            <span style="color:#f92672">return</span> <span style="color:#56b6c2">true</span>;
      }
      <span style="color:#f92672">return</span> <span style="color:#56b6c2">false</span>;
});</span></span>

13:List.Find 方法:搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List 中的第一个匹配元素。 

语法:public T Find(Predicate<T> match);

<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#e6c07b">string</span> listFind1 = mList.<span style="color:#e2b93d">Find(ListFind)</span>;  <span style="color:#b18eb1"><em>//委托给ListFind函数</em></span>
Console.<span style="color:#e2b93d">WriteLine(listFind1)</span>;</span></span>

14:List.FindLast 方法:搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List 中的最后一个匹配元素。

语法:public T FindLast(Predicate<T> match);

<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#e6c07b">string</span> listFind2 = mList.<span style="color:#e2b93d">FindLast(ListFind)</span>;  <span style="color:#b18eb1"><em>//委托给ListFind函数</em></span>
Console.<span style="color:#e2b93d">WriteLine(listFind2)</span>;</span></span>

15:List.TrueForAll方法:确定是否 List 中的每个元素都与指定的谓词所定义的条件相匹配。

语法:public bool TrueForAll(Predicate<T> match);

<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#e6c07b">bool</span> flag = mList.<span style="color:#e2b93d">TrueForAll(delegate(string name)</span>
{
      Console.<span style="color:#e2b93d">WriteLine(name)</span>;
      <span style="color:#f92672">if</span> (name.Length > <span style="color:#d19a66">3</span>)
      {
           return <span style="color:#56b6c2">true</span>;
      }
      return <span style="color:#56b6c2">false</span>;
});</span></span>

16:List.Take(n)方法:  获得前n行 返回值为IEnumetable<T>,T的类型与List<T>的类型一样:就是复制

<span style="background-color:#282c34"><span style="color:#abb2bf">            IEnumerable<<span style="color:#e6c07b">string</span>> mmmlist = mList.Take(<span style="color:#d19a66">3</span>);
            foreach (<span style="color:#e6c07b">string</span> <span style="color:#e6c07b">item</span> <span style="color:#f92672">in</span> mmmlist)
            {
                Console.WriteLine(<span style="color:#e6c07b">item</span>);
            }
            Console.WriteLine(<span style="color:#98c379">"
"</span>);</span></span>

17:List.Where方法:检索与指定谓词所定义的条件相匹配的所有元素。跟List.FindAll方法类似。

使用该方法定义存储集合的元素开头是:IEnumerable

<span style="background-color:#282c34"><span style="color:#abb2bf">            IEnumerable<<span style="color:#e6c07b">string</span>> whereList = mList.Where(ListFind);
            foreach (var <span style="color:#e6c07b">item</span> <span style="color:#f92672">in</span> whereList)
            {
                Console.WriteLine(<span style="color:#e6c07b">item</span>);
            }
            Console.WriteLine(<span style="color:#98c379">"
"</span>);</span></span>

18:List.RemoveAll方法:移除与指定的谓词所定义的条件相匹配的所有元素。

语法: public int RemoveAll(Predicate<T> match);

<span style="background-color:#282c34"><span style="color:#abb2bf">            mList.RemoveAll<span style="color:#e6c07b">(name</span> => {
                <span style="color:#f92672">if</span> <span style="color:#e6c07b">(name</span>.Length <= <span style="color:#d19a66">6</span>)
                {
                    <span style="color:#f92672">return</span><span style="color:#e6c07b"> true</span>;
                }
                <span style="color:#f92672">else</span> {
                    <span style="color:#f92672">return</span><span style="color:#e6c07b"> false</span>;
                }
            });
            foreach (var <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> mList)
            {
                Console.WriteLine(<span style="color:#d19a66">item</span>);
            }
            Console.WriteLine(<span style="color:#98c379">"
"</span>);</span></span>

测试使用全部代码:

<span style="background-color:#282c34"><span style="color:#abb2bf"><span style="color:#d19a66">using</span> <span style="color:#e6c07b">System</span>;
<span style="color:#d19a66">using</span> <span style="color:#e6c07b">System</span>.Collections.Generic;
<span style="color:#d19a66">using</span> <span style="color:#e6c07b">System</span>.Linq;
<span style="color:#d19a66">using</span> <span style="color:#e6c07b">System</span>.Text;
<span style="color:#d19a66">using</span> <span style="color:#e6c07b">System</span>.Threading.Tasks;
 
<span style="color:#d19a66">namespace</span> <span style="color:#61aeee">listys</span>
{
    <span style="color:#f92672">class</span> <span style="color:#e6c07b">Program</span>
    {
        <span style="color:#f92672">static</span> <span style="color:#d19a66">void</span> <span style="color:#e6c07b">Main</span>(<span style="color:#d19a66">string</span>[] <span style="color:#d19a66">args</span>)
        {
            <span style="color:#b18eb1"><em>// 声明列表</em></span>
            <span style="color:#b18eb1"><em>// List<string> mList = new List<string>();</em></span>
 
            <span style="color:#d19a66">string</span>[] <span style="color:#d19a66">arr</span> <span style="color:#f92672">=</span> { <span style="color:#98c379">"Ha"</span>, <span style="color:#98c379">"Hunter"</span>, <span style="color:#98c379">"Tom"</span>, <span style="color:#98c379">"Lily"</span>, <span style="color:#98c379">"Jay"</span>, <span style="color:#98c379">"Jim"</span>, <span style="color:#98c379">"Kuku"</span>, <span style="color:#98c379">"Locu"</span> };
            <span style="color:#e6c07b">List</span><span style="color:#f92672"><</span><span style="color:#d19a66">string</span><span style="color:#f92672">></span> <span style="color:#d19a66">mList</span> <span style="color:#f92672">=</span> <span style="color:#d19a66">new</span> <span style="color:#e6c07b">List</span><span style="color:#f92672"><</span><span style="color:#d19a66">string</span><span style="color:#f92672">></span>(<span style="color:#d19a66">arr</span>);
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">mList</span>.Count);
 
            <span style="color:#d19a66">mList</span>.Add(<span style="color:#98c379">"cmailla"</span>);
            <span style="color:#61aeee">foreach</span> (<span style="color:#d19a66">string</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
            <span style="color:#d19a66">mList</span>.Insert(<span style="color:#d19a66">0</span>,<span style="color:#98c379">"qwert"</span>);
            <span style="color:#61aeee">foreach</span> (<span style="color:#d19a66">string</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
            <span style="color:#d19a66">string</span>[] <span style="color:#d19a66">dfg</span> <span style="color:#f92672">=</span> { <span style="color:#98c379">"hiocvhi"</span>,<span style="color:#98c379">"jioosdfhio"</span>,<span style="color:#98c379">"erw"</span>};
            <span style="color:#d19a66">mList</span>.AddRange(<span style="color:#d19a66">dfg</span>);
            <span style="color:#61aeee">foreach</span> (<span style="color:#d19a66">string</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
            <span style="color:#d19a66">mList</span>.Remove(<span style="color:#98c379">"erw"</span>);
            <span style="color:#61aeee">foreach</span> (<span style="color:#d19a66">string</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
            <span style="color:#d19a66">mList</span>.RemoveAt(<span style="color:#d19a66">3</span>);
            <span style="color:#61aeee">foreach</span> (<span style="color:#d19a66">string</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
            <span style="color:#d19a66">mList</span>.RemoveRange(<span style="color:#d19a66">3</span>,<span style="color:#d19a66">5</span>);
            <span style="color:#61aeee">foreach</span> (<span style="color:#d19a66">string</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
            <span style="color:#d19a66">bool</span> <span style="color:#d19a66">s</span> <span style="color:#f92672">=</span> <span style="color:#d19a66">mList</span>.Contains(<span style="color:#98c379">"Ha"</span>);
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">s</span>);
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
            <span style="color:#d19a66">mList</span>.Sort();
            <span style="color:#61aeee">foreach</span> (<span style="color:#f92672">var</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
            <span style="color:#d19a66">mList</span>.Reverse();
            <span style="color:#61aeee">foreach</span> (<span style="color:#f92672">var</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
            <span style="color:#b18eb1"><em>/*mList.Clear();
            foreach (var item in mList)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("
");//*/</em></span>
 
            <span style="color:#b18eb1"><em>// 正常委托</em></span>
            <span style="color:#b18eb1"><em>// List<string> subList = mList.FindAll(ListFind); //委托给ListFind函数</em></span>
            <span style="color:#b18eb1"><em>// 匿名委托</em></span>
            <span style="color:#b18eb1"><em>/*List<string> subList = mList.FindAll( delegate(string name) {
                Console.WriteLine(name);
                if (name.Length > 3)
                {
                    return true;
                }
                return false;
            });//*/</em></span>
 
            <span style="color:#e6c07b">List</span><span style="color:#f92672"><</span><span style="color:#d19a66">string</span><span style="color:#f92672">></span> <span style="color:#d19a66">subList</span> <span style="color:#f92672">=</span> <span style="color:#d19a66">mList</span>.FindAll(<span style="color:#d19a66">name</span> <span style="color:#f92672">=</span><span style="color:#f92672">></span> {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">name</span>);
                <span style="color:#f92672">if</span> (<span style="color:#d19a66">name</span>.Length <span style="color:#f92672">></span> <span style="color:#d19a66">3</span>)
                {
                    <span style="color:#f92672">return</span> <span style="color:#56b6c2">true</span>;
                }
                <span style="color:#f92672">return</span> <span style="color:#56b6c2">false</span>;
            });
            <span style="color:#61aeee">foreach</span> (<span style="color:#d19a66">string</span> <span style="color:#d19a66">sss</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">subList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"element in subList: "</span> <span style="color:#f92672">+</span> <span style="color:#d19a66">sss</span>);
            }
 
            <span style="color:#d19a66">string</span> <span style="color:#d19a66">listFind1</span> <span style="color:#f92672">=</span> <span style="color:#d19a66">mList</span>.Find(<span style="color:#e6c07b">ListFind</span>);  <span style="color:#b18eb1"><em>//委托给ListFind函数</em></span>
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">listFind1</span>);
 
            <span style="color:#d19a66">string</span> <span style="color:#d19a66">listFind2</span> <span style="color:#f92672">=</span> <span style="color:#d19a66">mList</span>.FindLast(<span style="color:#e6c07b">ListFind</span>);  <span style="color:#b18eb1"><em>//委托给ListFind函数</em></span>
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">listFind2</span>);
 
            <span style="color:#d19a66">bool</span> <span style="color:#d19a66">flag</span> <span style="color:#f92672">=</span> <span style="color:#d19a66">mList</span>.TrueForAll(<span style="color:#61aeee">delegate</span>(string name)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">name</span>);
                <span style="color:#f92672">if</span> (<span style="color:#d19a66">name</span>.Length <span style="color:#f92672">></span> <span style="color:#d19a66">3</span>)
                {
                    <span style="color:#f92672">return</span> <span style="color:#56b6c2">true</span>;
                }
                <span style="color:#f92672">return</span> <span style="color:#56b6c2">false</span>;
            });
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">flag</span>);
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
 
            <span style="color:#e6c07b">IEnumerable</span><span style="color:#f92672"><</span><span style="color:#d19a66">string</span><span style="color:#f92672">></span> <span style="color:#d19a66">mmmlist</span> <span style="color:#f92672">=</span> <span style="color:#d19a66">mList</span>.Take(<span style="color:#d19a66">3</span>);
            <span style="color:#61aeee">foreach</span> (<span style="color:#d19a66">string</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mmmlist</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
 
            <span style="color:#e6c07b">IEnumerable</span><span style="color:#f92672"><</span><span style="color:#d19a66">string</span><span style="color:#f92672">></span> <span style="color:#d19a66">whereList</span> <span style="color:#f92672">=</span> <span style="color:#d19a66">mList</span>.Where(<span style="color:#e6c07b">ListFind</span>);
            <span style="color:#61aeee">foreach</span> (<span style="color:#f92672">var</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">whereList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
 
 
            <span style="color:#d19a66">mList</span>.RemoveAll(<span style="color:#d19a66">name</span> <span style="color:#f92672">=</span><span style="color:#f92672">></span> {
                <span style="color:#f92672">if</span> (<span style="color:#d19a66">name</span>.Length <span style="color:#f92672"><=</span> <span style="color:#d19a66">6</span>)
                {
                    <span style="color:#f92672">return</span> <span style="color:#56b6c2">true</span>;
                }
                <span style="color:#f92672">else</span> {
                    <span style="color:#f92672">return</span> <span style="color:#56b6c2">false</span>;
                }
            });
            <span style="color:#61aeee">foreach</span> (<span style="color:#f92672">var</span> <span style="color:#d19a66">item</span> <span style="color:#f92672">in</span> <span style="color:#d19a66">mList</span>)
            {
                <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#d19a66">item</span>);
            }
            <span style="color:#e6c07b">Console</span>.WriteLine(<span style="color:#98c379">"
"</span>);
            <span style="color:#e6c07b">Console</span>.ReadLine();
        }
        <span style="color:#b18eb1"><em>//ListFind函数</em></span>
        <span style="color:#d19a66">public</span> <span style="color:#f92672">static</span> <span style="color:#d19a66">bool</span> <span style="color:#e6c07b">ListFind</span>(<span style="color:#d19a66">string</span> <span style="color:#d19a66">name</span>)
        {
            <span style="color:#f92672">if</span> (<span style="color:#d19a66">name</span>.Length <span style="color:#f92672">></span> <span style="color:#d19a66">3</span>)
            {
                <span style="color:#f92672">return</span> <span style="color:#56b6c2">true</span>;
            }
            <span style="color:#f92672">return</span> <span style="color:#56b6c2">false</span>;
        }
    }
}</span></span>

有好的建议,请在下方输入你的评论。

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/736462.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Maven工程分模块开发讲解及入门案例

1.分模块开发的意义 一个模块只做自己对应的功能&#xff0c;提升开发效率&#xff0c;将一个工程拆分成若干个子模块方便之间相互调用&#xff0c;接口共享&#xff0c;降低耦合度提高代码复用率。 2.分模块开发入门案例 下面将domain这个模块从当前模块当中给拆分出来。 …

开心档之CSS 测验

目录 CSS 测验 CSS 测验 CSS测验是一种衡量前端开发人员对CSS的熟练程度的测试。通过CSS测验&#xff0c;可以评估一个人对CSS语言的掌握程度和应用能力&#xff0c;帮助公司或招聘方挑选合适的人才。下面将介绍如何进行CSS测验以及一些常见的CSS考题。 一、CSS测验的类型 1…

OpenCV 入门教程:寻找和绘制轮廓

OpenCV 入门教程&#xff1a;寻找和绘制轮廓 导语一、寻找轮廓二、绘制轮廓三、示例应用3.1 目标检测和定位3.2 图像分割 总结 导语 寻找和绘制轮廓是图像处理中常用的技术之一&#xff0c;用于识别、定位和分析图像中的目标区域。在 OpenCV 中&#xff0c;寻找和绘制轮廓可以…

「2024」预备研究生mem-行程问题

一、行程问题 二、课后题 往返 上山下山

LeetCode[75]颜色分类

难度:Medium 题目&#xff1a; 给定一个包含红色、白色和蓝色、共 n 个元素的数组 nums &#xff0c;原地对它们进行排序&#xff0c;使得相同颜色的元素相邻&#xff0c;并按照红色、白色、蓝色顺序排列。 我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。 必须在不使用库…

【前端面试专栏】用户输入网址到页面返回都发生了什么?

&#x1f431; 个人主页&#xff1a;不叫猫先生&#xff0c;公众号&#xff1a;前端舵手 &#x1f64b;‍♂️ 作者简介&#xff1a;2022年度博客之星前端领域TOP 2&#xff0c;前端领域优质作者、阿里云专家博主&#xff0c;专注于前端各领域技术&#xff0c;共同学习共同进步…

软件安全测试流程与方法分享(下)

安全测试是在IT软件产品的生命周期中&#xff0c;特别是产品开发基本完成到发布阶段&#xff0c;对产品进行检验以验证产品符合安全需求定义和产品质量标准的过程。安全是软件产品的一个重要特性&#xff0c;也是CNAS测试认证中非常重要的项目&#xff0c;本系列文章我们与大家…

linux 信号原理 信号处理设置signal, 信号发送kill,信号等待sigsuspend,信号阻塞sigprocmask,一网打尽信号使用

​专栏内容&#xff1a; postgresql内核源码分析 手写数据库toadb 并发编程 个人主页&#xff1a;我的主页 座右铭&#xff1a;天行健&#xff0c;君子以自强不息&#xff1b;地势坤&#xff0c;君子以厚德载物. 概述 信号是一种软中断的方式&#xff0c;让进程陷入中断处理调…

vector的resver和resize

#include <iostream> #include <vector> using namespace std; int main() {std::vector<std::vector<std::vector<int> > > a(2);//创建2个vector<vector<int> >类型的数组vector<int> vec;//vec.resize(10); //结果1vec.res…

【Linux之拿捏信号3】阻塞信号

文章目录 相关概念原理sigset_t信号集信号集操作函数sigprocmask系统调用sigpending 相关概念 实际执行信号的处理动作——信号递达Delivery&#xff08;例如自定义捕捉动作&#xff0c;core&#xff0c;Term终止进程的动作&#xff09;。信号从产生到递达之间的状态——信号未…

Verilog基础之十四、FIFO实现

目录 一、FIFO 1.1 定义 1.2 实现方式 1.3 实现原理 二、代码实现 三、仿真结果 3.1 复位阶段 3.2 写入阶段 3.3 读取阶段 3.4 同时读写或不读不写 四、参考资料 一、FIFO 1.1 定义 FIFO(First in First out)为先进先出队列&#xff0c;具有存储功能&#xff0c;…

一篇带你彻底搞懂线程池

目录 一、自定义线程池 1、产生背景 2、堵塞队列 3、线程池 4、拒绝策略 二、ThreadPoolExecuor 1、线程池状态 2、构造方法 3、newFixedThreadPool 4、newCachedThreadPool 5、newSingleThreadExecutor 6、提交任务 7、关闭线程池 三、异步模式之工作线程 1、定…

C-数据的储存(上)

文章目录 前言&#x1f31f;一、数据类型详细介绍&#x1f30f;1.内置类型&#x1f4ab;&#xff08;1&#xff09;.整形家族&#x1f4ab;&#xff08;2&#xff09;.浮点数家族&#x1f30f;2.构造类型&#xff08;也称自定义类型&#xff09;&#x1f30f;3.指针类型&#x…

OpenCV 入门教程:Haar特征分类器

OpenCV 入门教程&#xff1a; Haar 特征分类器 导语一、Haar特征分类器原理二、Haar特征分类器步骤三、示例应用总结 导语 Haar 特征分类器是图像处理中常用的目标检测算法&#xff0c;用于识别图像中的特定目标。该算法基于 Haar-like 特征模板&#xff0c;通过训练分类器来实…

ArcGIS PRO基础教程(一)

操作要求 1.面积为50-80亩 2.不能选在有耕地、园地内 3.坡度小于15度,高程在以下1930 4.距离水源地在300米以内 已知数据 1.等高线图 CONTOUR 2.土地利用图 parcel 3.水系图 water 操作步骤 创建工程,模板选地图就可以了(注:在arcgis pro中创建工程可以看作在arcg…

大火的ChatGPT与表格插件结合会有哪些意想不到的效果?

大火的ChatGPT与表格插件结合会有哪些意想不到的效果&#xff1f; 摘要&#xff1a;本文由葡萄城技术团队于CSDN原创并首发。转载请注明出处&#xff1a;葡萄城官网&#xff0c;葡萄城为开发者提供专业的开发工具、解决方案和服务&#xff0c;赋能开发者。 ChatGPT已经火了好…

前端全集Ⅰ---- HTML/CSS/JavaScript

一 介绍web开发 Web&#xff1a;全球广域网&#xff0c;也称万维网&#xff0c;能够通过浏览器访问的网站 Web网站的工作流程&#xff1a;&#xff08;前后端分离模式&#xff09; 网页有哪些组成&#xff1f; 文字、图片、视频、音频、超链接 前端代码通过浏览器的解析和渲…

3-exercises

解&#xff1a; &#xff08;1&#xff09;Create a tensor a from list(range(9)). Predict and then check the size, offset, and stride. 创建列表a 将其转化为张量 a.size&#xff1a;The size (or shape, in NumPy parlance) is a tuple indicating how many elements a…

脚本引流是什么?其实很好理解,就是利用软件脚本来引流,这种软件我们通常叫引流脚本

脚本引流是什么&#xff1f;其实很好理解&#xff0c;就是利用脚本来引流&#xff0c;这种软件我们通常叫引流脚本&#xff0c;引流脚本的研发就是结合了以往的那些加人软件&#xff0c;从中吸取了长处并且升级了功能&#xff0c;而且通过不断的测试改进&#xff0c;在今年的7月…

C# PaddleInference OCR文字识别(只识别)

说明 C# PaddleInference OCR文字识别&#xff08;只识别&#xff09;&#xff0c;没有文字区域检测、文字方向判断 测试图片是文字区域检测裁剪出来、处理过的图片 完整的OCR识别查看 C# PaddleInference OCR识别 学习研究Demo_天天代码码天天的博客-CSDN博客 效果 项目 …