List(T是一种数据类型,比如int、string、GameObject等)是一个非常有用的集合类型,它位于System.Collections.Generic命名空间下。它可以动态地调整大小,能够方便地添加、删除和访问元素。
cs
//创建List
List<int> intList = new List<int>();
List<string> stringList = new List<string>();
//添加元素
stringList.Add("Hello");
stringList.Add("World");
//插入元素
stringList.Insert(1, "Good");
//访问元素
string secondstr = stringList[1];
//获取元素数量
int count = stringList.Count;
//删除元素
stringList.Remove("World");
//删除指定索引位置的元素
stringList.RemoveAt(1);
//清空List元素
stringList.Clear();
//查找元素
int index = stringList.IndexOf("World");
//检查list是否包含元素
List<float> floatList = new List<float>() {1.0f, 2.0f, 3.0f};
bool containsTwo = floatList.Contains(2.0f);
//排序List中的元素
List<int> intList = new List<int>() {3, 1, 2};
intList.Sort();