C#中列表List的用法详解

系列文章目录

unity知识点



👉前言

List简介:
在 Unity 中,List(T是一种数据类型,比如int、string、GameObject等)是一个非常有用的集合类型,它位于System.Collections.Generic命名空间下。它可以动态地调整大小,能够方便地添加、删除和访问元素。

博客将会介绍Listd的使用方法。希望这篇博客对Unity的开发者有所帮助。
大家好,我是心疼你的一切,不定时更新Unity开发技巧,觉得有用记得一键三连哦。
欢迎点赞评论哦.下面就让我们进入正文吧 !


提示:以下是本篇文章正文内容,下面案例可供参考
在这里插入图片描述

👉一、常用API

👉1-1、创建List对象

示例:

     using System.Collections.Generic;
     public class Example : MonoBehaviour
     {
         void Start()
         {
             List<int> intList = new List<int>();
         }
     }

👉1-2、添加元素

语法:listName.Add(item);
示例:

       using System.Collections.Generic;
       public class Example : MonoBehaviour
       {
           void Start()
           {
               List<string> stringList = new List<string>();
               stringList.Add("Hello");
               stringList.Add("World");
           }
       }

👉1-3、将一个元素插入到指定索引位置

语法:listName.Insert(index, item);
示例:

       using System.Collections.Generic;
       public class Example : MonoBehaviour
       {
           void Start()
           {
               List<float> floatList = new List<float>();
               floatList.Add(1.0f);
               floatList.Add(3.0f);
               floatList.Insert(1, 2.0f); 
           }
       }

打印出来就是 1.0,2.0,3.0

👉1-4、访问List中元素

可以通过索引来访问List中的元素,索引从 0 开始。
语法:T element = listName[index];
示例:

     using System.Collections.Generic;
     public class Example : MonoBehaviour
     {
         void Start()
         {
             List<int> intList = new List<int>() {1, 2, 3};
             int secondElement = intList[1]; 
             Debug.Log(secondElement); 
         }
     }

👉1-5、获取元素数量

Count属性:返回List中元素的个数。
语法:int count = listName.Count;
示例:

       using System.Collections.Generic;
       public class Example : MonoBehaviour
       {
           void Start()
           {
               List<GameObject> gameObjectList = new List<GameObject>();
               gameObjectList.Add(new GameObject());
               gameObjectList.Add(new GameObject());
               int count = gameObjectList.Count; 
               Debug.Log(count); 
           }
       }

👉1-6、删除元素

Remove(T item):从List中删除指定的元素(如果有多个相同元素,只删除第一个)。
语法:listName.Remove(item);
示例:

       using System.Collections.Generic;
       public class Example : MonoBehaviour
       {
           void Start()
           {
               List<string> stringList = new List<string>(){"apple", "banana", "apple"};
               stringList.Remove("apple");
           }
       }

👉1-6、删除指定索引位置的元素

RemoveAt(int index):从List中删除指定索引位置的元素。
语法:listName.RemoveAt(index);
示例:

       using System.Collections.Generic;
       public class Example : MonoBehaviour
       {
           void Start()
           {
               List<int> intList = new List<int>() {1, 2, 3};
               intList.RemoveAt(1); 
           }
       }

👉1-7、清空List元素

Clear()方法:移除List中的所有元素。
语法:listName.Clear();
示例:

       using System.Collections.Generic;
       public class Example : MonoBehaviour
       {
           void Start()
           {
               List<Transform> transformList = new List<Transform>();
               transformList.Add(transform);
               transformList.Clear();
           }
       }

👉1-8、查找元素

IndexOf(T item):返回指定元素在List中的第一个索引,如果不存在则返回 - 1。
语法:int index = listName.IndexOf(item);
示例:

       using System.Collections.Generic;
       public class Example : MonoBehaviour
       {
           void Start()
           {
               List<string> stringList = new List<string>(){"apple", "banana", "apple"};
               int index = stringList.IndexOf("apple");
               int index1 = stringList.IndexOf("app");
                Debug.Log (index+"    "+index1);
           }
       }

👉1-9、检查list是否包含元素

1.Contains(T item):检查List是否包含指定元素,返回true或false。
语法:bool contains = listName.Contains(item);
示例:

       using System.Collections.Generic;
       public class Example : MonoBehaviour
       {
           void Start()
           {
               List<float> floatList = new List<float>() {1.0f, 2.0f, 3.0f};
               bool containsTwo = floatList.Contains(2.0f);
           }
       }

👉1-10、排序List中的元素

Sort()方法:对List中的元素进行排序。对于简单数据类型(如int、float、string等),会按照默认的排序规则进行排序。
语法:listName.Sort();
示例:

       using System.Collections.Generic;
       public class Example : MonoBehaviour
       {
           void Start()
           {
               List<int> intList = new List<int>() {3, 1, 2};
               intList.Sort();
           }
       }

打印出来是 1,2,3

👉1-11、反转List中的元素(倒序)

示例:

       using System.Collections.Generic;
       public class Example : MonoBehaviour
       {
           void Start()
           {
               List<int> intList = new List<int>() {1, 2, 3};
               //反转列表
               intList.Reverse();
           }
       }

打印出来是3,2,1

👉1-12、遍历List (for)

代码如下:

       using System.Collections.Generic;
       public class Example : MonoBehaviour
       {
           void Start()
           {
               List<string> stringList = new List<string>(){"apple", "banana", "cherry"};
               for (int i = 0; i < stringList.Count; i++)
               {
                   Debug.Log(stringList[i]);
               }
           }
       }

👉1-13、遍历List (foreach)

代码如下:

       using System.Collections.Generic;
       public class Example : MonoBehaviour
       {
           void Start()
           {
               List<GameObject> gameObjectList = new List<GameObject>();
               gameObjectList.Add(new GameObject("Object1"));
               gameObjectList.Add(new GameObject("Object2"));
               foreach (GameObject obj in gameObjectList)
               {
                   Debug.Log(obj.name);
               }
           }
       }

👉二、不常用API

👉2-1、List元素类型转换(ConvertAll)

功能:将List中的元素转换为另一种类型的List。
语法:List newList = oldList.ConvertAll(conversionDelegate);,其中conversionDelegate是一个转换函数,用于定义如何将T类型转换为TOutput类型。
示例:

   using System;
   using System.Collections.Generic;
   public class Example : MonoBehaviour
   {
       void Start()
       {
           List<int> intList = new List<int>() {1, 2, 3};
           List<string> stringList = intList.ConvertAll<string>(i => i.ToString());
       }
   }

👉2-2、判断是否包含与条件匹配的任何元素(Exists)

语法:bool exists = list.Exists(match);,其中match是一个Predicate类型的委托,用于定义匹配条件。
示例:

   using System;
   using System.Collections.Generic;
   public class Example : MonoBehaviour
   {
       void Start()
       {
           List<int> intList = new List<int>() {1, 2, 3};
           bool existsGreaterThanTwo = intList.Exists(i => i > 2);
       }
   }

👉2-3、判断与指定条件匹配的元素,并返回整个list中第一个匹配的元素(Find)

语法:T foundElement = list.Find(match);,其中match是Predicate类型的委托,用于定义匹配条件。
示例:

   using System;
   using System.Collections.Generic;
   public class Example : MonoBehaviour
   {
       void Start()
       {
           List<int> intList = new List<int>() {1, 2, 3};
           int foundElement = intList.Find(i => i > 1);
       }
   }

👉2-4、检索与指定条件匹配的所有元素(FindAll)

语法:List foundElements = list.FindAll(match);,其中match是Predicate类型的委托,用于定义匹配条件。
示例:

   using System;
   using System.Collections.Generic;
   public class Example : MonoBehaviour
   {
       void Start()
       {
           List<int> intList = new List<int>() {1, 2, 3, 4, 5};
           List<int> evenNumbers = intList.FindAll(i => i % 2 == 0);
       }
   }

👉2-5、检索与指定条件匹配的元素,并返回list中第一个匹配元素的索引(FindIndex)

功能:搜索与指定谓词定义的条件匹配的元素,并返回整个List中第一个匹配元素的从零开始的索引。
语法:int index = list.FindIndex(match);,其中match是Predicate类型的委托,用于定义匹配条件。
示例:

   using System;
   using System.Collections.Generic;
   public class Example : MonoBehaviour
   {
       void Start()
       {
           List<int> intList = new List<int>() {1, 2, 3};
           int index = intList.FindIndex(i => i > 1);
       }
   }

👉2-6、检索与指定条件匹配的元素,并返回list中最后一个匹配元素(FindLast)

功能:搜索与指定谓词定义的条件匹配的元素,并返回整个List中的最后一个匹配元素。
语法:T foundElement = list.FindLast(match);,其中match是Predicate类型的委托,用于定义匹配条件。
示例:

   using System;
   using System.Collections.Generic;
   public class Example : MonoBehaviour
   {
       void Start()
       {
           List<int> intList = new List<int>() {1, 2, 3};
           int foundElement = intList.FindLast(i => i < 3);
       }
   }

👉2-7、检索与指定条件匹配的元素,并返回list中最后一个匹配元素的索引(FindLastIndex)

功能:搜索与指定谓词定义的条件匹配的元素,并返回整个List中最后一个匹配元素的从零开始的索引。
语法:int index = list.FindLastIndex(match);,其中match是Predicate类型的委托,用于定义匹配条件。
示例:

   using System;
   using System.Collections.Generic;
   public class Example : MonoBehaviour
   {
       void Start()
       {
           List<int> intList = new List<int>() {1, 2, 3};
           int index = intList.FindLastIndex(i => i < 3);
       }
   }

👉2-8、对列表中的每个元素执行指定操作(ForEach)

语法:list.ForEach(action);,其中action是一个Action类型的委托,用于定义要对每个元素执行的操作。
示例:

   using System;
   using System.Collections.Generic;
   public class Example : MonoBehaviour
   {
       void Start()
       {
           List<int> intList = new List<int>() {1, 2, 3};
           intList.ForEach(i => Debug.Log(i));
       }
   }

👉2-9、创建一个包含list中指定范围的元素生成新的列表(GetRange)

功能:创建一个包含源List中指定范围的元素的新List。
语法:List newList = list.GetRange(index, count);,其中index是起始索引,count是要包含的元素数量。
示例:

   using System;
   using System.Collections.Generic;
   public class Example : MonoBehaviour
   {
       void Start()
       {
           List<int> intList = new List<int>() {1, 2, 3, 4, 5};
           List<int> subList = intList.GetRange(1, 3);
       }
   }

👉2-10、判断list中的所有元素是否与条件匹配(TrueForAll)

语法:bool allMatch = list.TrueForAll(match);,其中match是Predicate类型的委托,用于定义匹配条件。
示例:

   using System;
   using System.Collections.Generic;
   public class Example : MonoBehaviour
   {
       void Start()
       {
           List<int> intList = new List<int>() {2, 4, 6};
           bool allEven = intList.TrueForAll(i => i % 2 == 0);
       }
   }

请添加图片描述

👉壁纸分享

请添加图片描述
请添加图片描述


👉总结

本次总结的就是List的api详解, 有需要会继续增加功能
如能帮助到你,就帮忙点个赞吧,三连更好哦,谢谢
你的点赞就是对博主的支持,有问题记得留言评论哦!
不定时更新Unity开发技巧,觉得有用记得一键三连哦。么么哒!

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心疼你的一切

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值