C#泛型类之List

    技术2022-05-20  63

    1、        定义

    System.Collections.Generic.List<T>类表示可通过索引访问的对象的强类型列表。提供用于对列表进行 搜索、排序和操作的方法。T为类型参数,代表列表中元素的类型。该类实现了IList<T>泛型接口,是ArrayList类的泛型等效类, 其大小可按需动态增加。

    2.构造函数

    名称

    说明

    List<T>()

    初始化 List<T> 类的新实例,该实例为空并且具有默认初始容量(0)。

    List<T>(IEnumerable<T>)

    初始化 List<T> 类的新实例,该实例包含从指定集合复制的元素并且具有足够的容量来容纳所复制的元素。

    List<T>(Int32)

    始化 List<T> 类的新实例,该实例为空并且具有指定的初始容量。

    说明:默认向 List<T> 添加元素时,将通过重新分配内部数组,根据需要自动增大容量。如果可以估计集合的大小,那么当指定初始容量后,将无需在向 List<T> 中添加元素时执行大量的大小调整操作。这样可提高性能。

     

    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.示例程序

    class Program { static void Main( string [] args) { #region List<T>类常用的方法 List < string > dinosaurs = new List < string > (); // 创建一个string的List集合 Console.WriteLine( " /nCapacity: {0} " , dinosaurs.Capacity); // 输出默认容量的大小 dinosaurs.Add( " Tyrannosaurus " ); // 向集合添加元素 dinosaurs.Add( " Amargasaurus " ); dinosaurs.Add( " Mamenchisaurus " ); dinosaurs.Add( " Deinonychus " ); dinosaurs.Add( " Compsognathus " ); 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.备注

    1、  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 没有实现其中任一个接口,则不存在默认比较器,并且必须显式提供比较器或比较委托。

    2、  List<T> 不保证是排序的。在执行要求 List<T> 已排序的操作(例如 BinarySearch)之前,您必须对 List<T> 进行排序。

    3、  List<T> 不保证是排序的。在执行要求 List<T> 已排序的操作(例如 BinarySearch)之前,您必须对 List<T> 进行排序。

    4、  使用整数索引可以访问此集合中的元素。此集合中的索引从零开始。

    5、  List<T> 接受 null 作为引用类型的有效值并且允许有重复的元素。

    6、  大多数情况下List<T>执行得更好并且是类型安全的,可以替换ArrayList,但是如果对类型 T 使用值类型,则编译器将特别针对该值类型生成 List<T> 类的实现。这意味着不必对 List<T> 对象的列表元素进行装箱就可以使用该元素,并且在创建大约 500 个列表元素之后,不对列表元素装箱所节省的内存将大于生成该类实现所使用的内存。如果创建小于500个元素,建议使用ArrayList.


    最新回复(0)