一、 定义
System.Collections.Generic.List<T>类表示可经过索引访问的对象的强类型列表。提供用于对列表进行搜索、排序和操做的方法。T为类型参数,表明列表中元素的类型。该类实现了IList<T>泛型接口,是ArrayList类的泛型等效类,其大小可按需动态增长。html
2.构造函数
名称数组 |
说明安全 |
List<T>()数据结构 |
初始化 List<T> 类的新实例,该实例为空而且具备默认初始容量(0)。dom |
List<T>(IEnumerable<T>)ide |
初始化 List<T> 类的新实例,该实例包含从指定集合复制的元素而且具备足够的容量来容纳所复制的元素。函数 |
List<T>(Int32)post |
始化 List<T> 类的新实例,该实例为空而且具备指定的初始容量。性能 |
说明:默认向 List<T> 添加元素时,将经过从新分配内部数组,根据须要自动增大容量。若是能够估计集合的大小,那么当指定初始容量后,将无需在向 List<T> 中添加元素时执行大量的大小调整操做。这样可提升性能。url
3. List<T>方法
名称 |
说明 |
Add |
将对象添加到 List<T> 的结尾处。 |
AddRange |
将指定集合的元素添加到 List<T> 的末尾。 |
AsReadOnly |
返回当前集合的只读 IList<T> 包装。 |
BinarySearch(T) |
使用默认的比较器在整个已排序的 List<T> 中搜索元素,并返回该元素从零开始的索引。 |
BinarySearch(T, IComparer<T>) |
使用指定的比较器在整个已排序的 List<T> 中搜索元素,并返回该元素从零开始的索引。 |
BinarySearch(Int32, Int32, T, IComparer<T>) |
使用指定的比较器在已排序 List<T> 的某个元素范围中搜索元素,并返回该元素从零开始的索引。 |
Clear |
从 List<T> 中移除全部元素。 |
Contains |
肯定某元素是否在 List<T> 中。 |
ConvertAll<TOutput> |
将当前 List<T> 中的元素转换为另外一种类型,并返回包含转换后的元素的列表。 |
CopyTo(T[]) |
将整个 List<T> 复制到兼容的一维数组中,从目标数组的开头开始放置。 |
Exists |
肯定 List<T> 是否包含与指定谓词所定义的条件相匹配的元素。 |
Find |
搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List<T> 中的第一个匹配元素。 |
FindIndex(Predicate<T>) |
搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List<T> 中第一个匹配元素的从零开始的索引。 |
ForEach |
对 List<T> 的每一个元素执行指定操做。 |
GetEnumerator |
返回循环访问 List<T> 的枚举器。 |
IndexOf(T) |
搜索指定的对象,并返回整个 List<T> 中第一个匹配项的从零开始的索引。 |
Insert |
将元素插入 List<T> 的指定索引处。 |
InsertRange |
将集合中的某个元素插入 List<T> 的指定索引处。 |
LastIndexOf(T) |
搜索指定的对象,并返回整个 List<T> 中最后一个匹配项的从零开始的索引。 |
Remove |
从 List<T> 中移除特定对象的第一个匹配项。 |
Reverse() |
将整个 List<T> 中元素的顺序反转。 |
Sort() |
使用默认比较器对整个 List<T> 中的元素进行排序。 |
TrimExcess |
将容量设置为 List<T> 中的实际元素数目(若是该数目小于某个阈值)。 |
TrueForAll |
肯定是否 List<T> 中的每一个元素都与指定的谓词所定义的条件相匹配。 |
说明:上述方法说明中有用到“谓词”,谓词就是Predicate<T> 委托,它表明一组方法,该方法定义一组条件,并肯定指定的参数对象是否符合这些条件,具体的参见示例程序。
4. List<T>属性
名称 |
说明 |
Capacity |
获取或设置该内部数据结构在不调整大小的状况下可以容纳的元素总数。 |
Count |
获取 List<T> 中实际包含的元素数。 |
说明:Capacity 是 List<T> 在须要调整大小以前能够存储的元素数,Count 则是 List<T> 中实际存储的元素数。
5.示例程序

Console.WriteLine(); foreach ( string dinosaur in dinosaurs) // 打印集合中的元素 { Console.WriteLine(dinosaur); }
Console.WriteLine( " \nCapacity: {0} " , dinosaurs.Capacity); Console.WriteLine( " Count: {0} " , dinosaurs.Count); // 输出集合中实际元素的数量 Console.WriteLine( " \nContains(\ " Deinonychus\ " ): {0} " , dinosaurs.Contains( " Deinonychus " )); // 判断集合中是否包含某个元素 Console.WriteLine( " \nInsert(2, \ " Compsognathus\ " ) " ); dinosaurs.Insert( 2 , " Compsognathus " ); // 将元素插入到集合的指定索引出,容许插入重复的元素 Console.WriteLine(); foreach ( string dinosaur in dinosaurs) // 打印集合中的元素 { Console.WriteLine(dinosaur); } Console.WriteLine( " \ndinosaurs[3]: {0} " , dinosaurs[ 3 ]); // 输出集合中索引为3的元素 Console.WriteLine( " \nRemove(\ " Compsognathus\ " ) " ); dinosaurs.Remove( " Compsognathus " ); // 移除集合中第一个匹配的元素 Console.WriteLine(); foreach ( string dinosaur in dinosaurs) // 打印集合中的元素 { Console.WriteLine(dinosaur); } dinosaurs.TrimExcess(); // 减少容量以匹配计数,而后显示 Capacity 和 Count 属性。若是未用容量已经小于总容量的 10%,则列表容量不会进行调整。 Console.WriteLine( " \nTrimExcess() " ); Console.WriteLine( " Capacity: {0} " , dinosaurs.Capacity); Console.WriteLine( " Count: {0} " , dinosaurs.Count);
dinosaurs.Clear(); // 移除列表中的全部项,而后显示 Capacity 和 Count 属性。 Console.WriteLine( " \nClear() " ); Console.WriteLine( " Capacity: {0} " , dinosaurs.Capacity); Console.WriteLine( " Count: {0} " , dinosaurs.Count); #endregion
#region List<T>类的新增方法
List < string > dinosaurs1 = new List < string > (); // 建立一个string的List集合 dinosaurs1.Add( " Compsognathus " ); // 向集合添加元素 dinosaurs1.Add( " Amargasaurus " ); dinosaurs1.Add( " Oviraptor " ); dinosaurs1.Add( " Velociraptor " ); dinosaurs1.Add( " Deinonychus " ); dinosaurs1.Add( " Dilophosaurus " ); dinosaurs1.Add( " Gallimimus " ); dinosaurs1.Add( " Triceratops " );
Console.WriteLine(); foreach ( string dinosaur in dinosaurs1) { Console.WriteLine(dinosaur); }
Console.WriteLine( " \nTrueForAll(EndsWithSaurus): {0} " , dinosaurs1.TrueForAll(EndsWithSaurus)); // 肯定集合中的每一个元素是否都与指定的谓词所定义的条件相匹配 Console.WriteLine( " \nFind(EndsWithSaurus): {0} " , dinosaurs1.Find(EndsWithSaurus)); // 搜索与指定谓词条件相匹配的第一个元素 Console.WriteLine( " \nFindLast(EndsWithSaurus): {0} " , dinosaurs1.FindLast(EndsWithSaurus)); // 搜索与指定谓词条件相匹配的最后一个元素 Console.WriteLine( " \nFindAll(EndsWithSaurus): " ); List < string > sublist = dinosaurs1.FindAll(EndsWithSaurus); // 检索与指定谓词定义的条件匹配的全部元素 foreach ( string dinosaur in sublist) // 打印集合 { Console.WriteLine(dinosaur); }
Console.WriteLine( " \n{0} elements removed by RemoveAll(EndsWithSaurus). " , dinosaurs1.RemoveAll(EndsWithSaurus)); // 移除与指定谓词定义的条件匹配的全部元素 Console.WriteLine( " \nList now contains: " ); foreach ( string dinosaur in dinosaurs1) // 打印集合 { Console.WriteLine(dinosaur); }
Console.WriteLine( " \nExists(EndsWithSaurus): {0} " , dinosaurs1.Exists(EndsWithSaurus)); // 该方法从头开始遍历该列表,依次将每一个元素传递给 EndsWithSaurus 方法。若是 EndsWithSaurus 方法针对任何元素返回 true,搜索即中止 dinosaurs1.Sort(); // 对集合中的元素排序 dinosaurs1.Reverse(); // 将集合中的元素顺序反转 dinosaurs1.ForEach(Print); // 对集合中的每一个元素执行指定的方法(如Print方法) Console.Read(); #endregion }
// 搜索谓词方法,该方法接受一个字符串做为参数,并返回一个布尔值,指示输入的字符串是否以“saurus”结尾。 private static bool EndsWithSaurus(String s) { if ((s.Length > 5 ) && (s.Substring(s.Length - 6 ).ToLower() == " saurus " )) { return true ; } else { return false ; } }
// 定义打印集合的方法 private static void Print( string s) { Console.WriteLine(s); }
}
6.备注
一、 List<T> 类既使用相等比较器又使用排序比较器。
- 诸如 Contains、IndexOf、LastIndexOf 和 Remove 这样的方法对列表元素使用相等比较器。类型 T 的默认相等比较器按以下方式肯定。若是类型 T 实现 IEquatable<T> 泛型接口,则相等比较器为该接口的 Equals(T) 方法;不然,默认相等比较器为 Object.Equals(Object)。
- 诸如 BinarySearch 和 Sort 这样的方法对列表元素使用排序比较器。类型 T 的默认比较器按以下方式肯定。若是类型 T 实现 IComparable<T> 泛型接口,则默认比较器为该接口的 CompareTo(T) 方法;不然,若是类型 T 实现非泛型 IComparable 接口,则默认比较器为该接口的 CompareTo(Object) 方法。若是类型 T 没有实现其中任一个接口,则不存在默认比较器,而且必须显式提供比较器或比较委托。
二、 List<T> 不保证是排序的。在执行要求 List<T> 已排序的操做(例如 BinarySearch)以前,您必须对 List<T> 进行排序。
三、 List<T> 不保证是排序的。在执行要求 List<T> 已排序的操做(例如 BinarySearch)以前,您必须对 List<T> 进行排序。
四、 使用整数索引能够访问此集合中的元素。此集合中的索引从零开始。
五、 List<T> 接受 null 做为引用类型的有效值而且容许有重复的元素。
六、 大多数状况下List<T>执行得更好而且是类型安全的,能够替换ArrayList,可是若是对类型 T 使用值类型,则编译器将特别针对该值类型生成 List<T> 类的实现。这意味着没必要对 List<T> 对象的列表元素进行装箱就可使用该元素,而且在建立大约 500 个列表元素以后,不对列表元素装箱所节省的内存将大于生成该类实现所使用的内存。若是建立小于500个元素,建议使用ArrayList.