C#(五十八)之C#List

这篇具有很好参考价值的文章主要介绍了C#(五十八)之C#List。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前几天,看同事写的代码中有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>

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

C#(五十八)之C#List,windows

 文章来源地址https://www.toymoban.com/news/detail-550820.html

到了这里,关于C#(五十八)之C#List的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • npm与node版本不匹配问题解决思路(一百五十八)

    1.报错 npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: ‘electron-packager@17.1.1’, npm WARN EBADENGINE required: { node: ‘= 14.17.5’ }, npm WARN EBADENGINE current: { node: ‘v12.22.9’, npm: ‘8.5.1’ } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: ‘mdui@1.0.2’, npm WARN E

    2024年02月09日
    浏览(51)
  • AIGC内容分享(五十八):AIGC数据跨境的法律监管和合规路径

    目录 一、引言 二、AIGC数据出境主要场景分析 三、数据出境法律监管和合规路径 四、对AIGC数据出境的合规建议 在生成式人工智能(Generative AI,下称 “AIGC” )技术的发展和应用过程中,相关法律监管问题一直备受各国政府关注。例如,AIGC相关技术和硬件的出口管制问题、

    2024年01月25日
    浏览(53)
  • 大数据Flink(五十八):Flink on Yarn的三种部署方式介绍

    文章目录 Flink on Yarn的三种部署方式介绍 一、​​​​​​​Session模式

    2024年02月13日
    浏览(43)
  • 【Java基础教程】(四十八)集合体系篇 · 上:全面解析 Collection、List、Set常用子接口及集合元素迭代遍历方式~【文末送书】

    掌握 Java 设置类集的主要目的以及核心接口的使用; 掌握 Collection 接口的作用及主要操作方法; 掌握 Collection 子接口 List、Set 的区别及常用子类的使用; 掌握 Map 接口的定义及使用; 掌握集合的4种输出操作语法结构; 掌握 Properties类的使用 ; 了解类集工具类 Collections 的作

    2024年02月15日
    浏览(57)
  • 部署问题集合(十八)Windows环境下使用两个Tomcat

    Tomcat镜像下载地址:https://mirrors.cnnic.cn/apache/tomcat/ 进入如下地址:zip的是压缩版,exe是安装版 第一步:编辑conf/server.xml文件,修改三个端口,有些版本改两个就行,如下,改成不冲突的端口即可 第二步:编辑bin/startup.bat文件,注释掉第二句话即可 注意:如果没有配置Java的

    2024年02月15日
    浏览(64)
  • c# list集合克隆

    在C#中,List集合是一种泛型集合,可以存储任何类型的对象。克隆一个List集合可以通过以下几种方式实现: 使用List的构造函数 使用List的构造函数可以创建一个新的List对象,并将原始List中的元素复制到新List中。例如: 在上面的代码中, list2 是一个新的List对象,它使用

    2024年02月06日
    浏览(41)
  • C# List 详解一

    目录 一、概述 二、构造函数 1.List()   2.List(IEnumerable)     3.List(Int32)     三、属性 1.Capacity     2.Count     3.Item[Int32]     四、方法 1.Add(T)     2.AddRange(IEnumerable)     3.AsReadOnly()     4.BinarySearch(T) C# List 文档(一) 1.Add(T),2.AddRange(IEnumerable),3.AsReadOnly(),4.BinarySearch(

    2024年02月16日
    浏览(43)
  • C# 自定义List

    目录 一、需求 二、List 常用功能 三、自定义List 四、测试 1.Add 2.Clear 3.Contains 4.IndexOf 5.Insert 6.Remove 7.RemoveAt 结束 微软官方的 List 在命名空间 System.Collections.Generic 中,在平时的开发中 List 用的特别多,在用的时候我们基本不会考虑在 List 中内部是怎么写的,于是,我也写了一

    2023年04月24日
    浏览(38)
  • C# List 详解二

    目录 5.Clear()     6.Contains(T)     7.ConvertAll(Converter)    ,toutput 8.CopyTo(Int32, T[], Int32, Int32)     9.CopyTo(T[])     10.CopyTo(T[], Int32)     C# List 文档(一) 1.Add(T),2.AddRange(IEnumerable),3.AsReadOnly(),4.BinarySearch(T), C# List 文档(二) 5.Clear(),6.Contains(T),7.ConvertAll(Converter),8.CopyT

    2024年02月16日
    浏览(45)
  • C# List 详解五

    目录 26.GetType()     27.IndexOf(T)     28.IndexOf(T, Int32)     29.IndexOf(T, Int32, Int32)     30.Insert(Int32, T)     31.InsertRange(Int32, IEnumerable)     32.LastIndexOf(T)     33.LastIndexOf(T, Int32)     34.LastIndexOf(T, Int32, Int32)     C# List 文档(一) 1.Add(T),2.AddRange(IEnumerable),3.AsReadOnly(),4.Bina

    2024年02月16日
    浏览(51)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包