C# 三十七、泛型集合类+List<T>

泛型集合类

泛型集合类允许用户创建强类型集合,它能够提供比非泛型强类型集合更好的类型安全性和性能。

泛型集合包含的类与非泛型包含的类基本一一对应,是来取代非泛型集合对应的类的。

主要泛型集合类有:

List<T>

Dictionary<K,V>

Queue<T>

SortedList<K,V>

Stack<T>

List<T>

定义 

语法格式:

一、初始化 System.Collections.Generic.List`1 类的新实例,该实例为空并且具有默认初始容量。

List<数据类型> 标识符 = new List<数据类型>();

二、初始化 System.Collections.Generic.List`1 类的新实例,该实例为空并且具有指定的初始容量。
List<数据类型> 标识符 = new List<数据类型>(新列表最初可以存储的元素数);

 三、初始化 System.Collections.Generic.List`1 类的新实例,该实例包含从指定集合复制的元素并且具有足够的容量来容纳所复制的元素。
List<数据类型> 标识符 = new List<数据类型>(其元素被复制到新列表的原列表);

List<string> list1 = new List<string>();
List<string> list2 = new List<string>(5);
List<string> list3 = new List<string>(list1);

List<T>类的一些常用的属性

public int Count { get; }

官方摘要:获取 System.Collections.Generic.List`1 中实际包含的元素数。

返回结果:System.Collections.Generic.List`1中实际包含的元素数。

简单理解:获取实际包含的元素个数。

代码示例:

List<string> list = new List<string>() {"aa","bb","cc"};

Console.WriteLine(list.Count);

--->
3

public int Capacity { get; set; }

 官方摘要:获取或设置该内部数据结构在不调整大小的情况下能够容纳的元素总数。

返回结果:在需要调整大小之前 System.Collections.Generic.List`1 可包含的元素数目。

简单理解:获取可包含的元素个数。

代码示例:

List<string> list = new List<string>(5) { "aa", "bb", "cc" };

Console.WriteLine(list.Capacity);

--->
5

List<T>类的一些常用的方法

 public void Add(T item);

官方摘要:将对象添加到 System.Collections.Generic.List`1 的结尾处。

参数说明:

  • item:要添加到 System.Collections.Generic.List`1 末尾的对象。 对于引用类型,该值可以为 null。

简单理解:添加元素。

代码示例:

List<string> list = new List<string>() { "aa", "bb", "cc" };

list.Add("dd");

foreach (var item in list)
{
    Console.WriteLine(item);
}

--->
aa
bb
cc
dd

public void AddRange(IEnumerable<T> collection);

 官方摘要:将指定集合的元素添加到 System.Collections.Generic.List`1 的末尾。

参数说明:

  • collection:应将其元素添加到 System.Collections.Generic.List`1 的末尾的集合。 集合自身不能为 null,但它可以包含为 null的元素(如果类型 T 为引用类型)。

简单理解:添加其他集合元素到此集合。

代码示例:

List<string> list1 = new List<string>() { "aa", "bb", "cc" };
List<string> list2 = new List<string>() { "dd", "ee", "ff" };

list1.AddRange(list2);

foreach (var item in list1)
{
    Console.WriteLine(item);
}

--->
aa
bb
cc
dd
ee
ff

public int BinarySearch(T item);

官方摘要:使用默认的比较器在整个已排序的 System.Collections.Generic.List`1 中搜索元素,并返回该元素从零开始的索引。

参数说明:

  • item:要定位的对象。 对于引用类型,该值可以为 null。

返回结果:如果找到 item,则为已排序的 System.Collections.Generic.List`1 中 item 的从零开始的索引;否则为一个负数,该负数是大于item 的下一个元素的索引的按位求补。如果没有更大的元素,则为 System.Collections.Generic.List`1.Count 的按位求补。

简单理解:二分法查找元素。

代码示例:(更多重载,不一一列出)

List<int> list = new List<int>() { 11,12,13 };
Console.WriteLine(list.BinarySearch(11));

--->
0

public void Clear();

官方摘要:从 System.Collections.Generic.List`1 中移除所有元素。

简单理解:移除所有元素。

代码示例:

List<int> list = new List<int>() { 11,12,13 };

list.Clear();

list.Add(14);

foreach (var item in list)
{
    Console.WriteLine(item);
}

--->
14

 public bool Contains(T item);

官方摘要:确定某元素是否在 System.Collections.Generic.List`1 中。

参数说明:

  • item:要在 System.Collections.Generic.List`1 中定位的对象。 对于引用类型,该值可以为 null。

返回结果:如果在System.Collections.Generic.List`1中找到 item,则为true;否则为 false。

简单理解:判断是否包含指定元素。

代码示例:

List<int> list = new List<int>() { 11,12,13 };

Console.WriteLine(list.Contains(11));

--->
True

 public void CopyTo(int index, T[] array, int arrayIndex, int count);

官方摘要:从目标数组的指定索引处开始,将元素的范围从 System.Collections.Generic.List`1 复制到兼容的一维数组。

参数说明:

  • index:即从源 System.Collections.Generic.List`1 中从零开始的开始复制的索引。
  • array:一维 System.Array,它是从 System.Collections.Generic.List`1 复制的元素的目标。 System.Array必须具有从零开始的索引。
  • arrayIndex:array 中从零开始的索引,从此处开始复制。
  • count:要复制的元素数。

简单理解:将指定元素复制到指定数组的指定位置。

代码示例:(更多重载,不一一列出)

List<int> list = new List<int>() { 11,12,13 };

int[] arr = new int[3];

list.CopyTo(0,arr,0,3);

foreach (var item in arr)
{
    Console.WriteLine(item);
}

--->
11
12
13

public List<T> GetRange(int index, int count);

 官方摘要:在源 System.Collections.Generic.List`1 中创建元素范围的浅表复制。

参数说明:

  • index:范围开始处的从零开始的 System.Collections.Generic.List`1 索引。
  • count:范围中的元素数。

返回结果:源 System.Collections.Generic.List`1 中的元素范围的浅表副本复制。

简单理解:将指定元素复制到新集合。

代码示例:

List<int> list1 = new List<int>() { 11,12,13 };

List<int> list2 = list1.GetRange(0,2);

foreach (var item in list2)
{
    Console.WriteLine(item);
}

--->
11
12

public int IndexOf(T item, int index, int count);

官方摘要:搜索指定对象并返回 System.Collections.Generic.List`1 中从指定索引开始并包含指定元素数的这部分元素中第一个匹配项的从零开始索引。

参数说明:

  • item:要在 System.Collections.Generic.List`1 中定位的对象。 对于引用类型,该值可以为 null。
  • index:从零开始的搜索的起始索引。 空列表中 0(零)为有效值。
  • count:要搜索的部分中的元素数。

返回结果:如果在 System.Collections.Generic.List`1 中从 index 开始并包含 count 个元素的元素范围内找到 item 的第一个匹配项,则为该项的从零开始的索引;否则为-1。

简单理解:查找指定元素,返回第一个匹配项下标。

代码示例:(更多重载,不一一列出)

List<int> list = new List<int>() { 11,12,13 };

Console.WriteLine(list.IndexOf(12,0,3));

--->
1

public void Insert(int index, T item);

官方摘要:将元素插入 System.Collections.Generic.List`1 的指定索引处。

参数说明:

  • index:应插入 item 的从零开始的索引。
  • item:要插入的对象。 对于引用类型,该值可以为 null。

简单理解:在指定位置插入元素。

代码示例:

List<int> list = new List<int>() { 11,12,13 };

list.Insert(2,14);

foreach (var item in list)
{
    Console.WriteLine(item);
}

--->
11
12
14
13

public void InsertRange(int index, IEnumerable<T> collection);

官方摘要:将集合中的元素插入 System.Collections.Generic.List`1 的指定索引处。

参数说明:

  • index:应在此处插入新元素的从零开始的索引。
  • collection:应将其元素插入到 System.Collections.Generic.List`1 中的集合。 集合自身不能为 null,但它可以包含为 null 的元素(如果类型T 为引用类型)。

简单理解:在指定位置插入某集合的元素。

代码示例:

List<int> list1 = new List<int>() { 11, 12, 13 };
List<int> list2 = new List<int>() { 14, 15 };

list1.InsertRange(0,list2);

foreach (var item in list1)
{
    Console.WriteLine(item);
}

--->
14
15
11
12
13

public int LastIndexOf(T item, int index, int count);

官方摘要:搜索指定对象并返回 System.Collections.Generic.List`1 中到指定索引为止包含指定元素数的这部分元素中最后一个匹配项的从零开始索引。

参数说明:

  • item:要在 System.Collections.Generic.List`1 中定位的对象。 对于引用类型,该值可以为 null。
  • index:向后搜索的从零开始的起始索引。
  • count:要搜索的部分中的元素数。

返回结果:如果找到包含 count 个元素、到 index 为止的索引,则为 System.Collections.Generic.List`1 中元素范围内 item  的最后一个匹配项的从零开始的索引;否则为 -1。

简单理解:在指定范围从后向前查找某指定元素。

代码示例:(更多重载,不一一列出)

List<int> list = new List<int>() { 11, 12, 13 };

Console.WriteLine(list.LastIndexOf(13));

--->
2

public bool Remove(T item);

官方摘要:从 System.Collections.Generic.List`1 中移除特定对象的第一个匹配项。

参数说明:

  • item:要从 System.Collections.Generic.List`1 中删除的对象。 对于引用类型,该值可以为 null。

返回结果:如果成功移除了 item,则为 true;否则为 false。 如果在System.Collections.Generic.List`1 中没有找到 item,则此方法也会返回false 。

简单理解:移除特定对象的第一个匹配项。

代码示例:

List<int> list = new List<int>() {11, 12, 13};

Console.WriteLine(list.Remove(12));

foreach (var item in list)
{
    Console.WriteLine(item);
}

--->
True
11
13

public void RemoveAt(int index);

官方摘要:移除 System.Collections.Generic.List`1 的指定索引处的元素。

参数说明:

  • index:要移除的元素的从零开始的索引。

简单理解:移除指定下标的元素。

代码示例:

List<int> list = new List<int>() {11, 12, 13};

list.RemoveAt(1);

foreach (var item in list)
{
    Console.WriteLine(item);
}

--->
11
13

public void RemoveRange(int index, int count);

官方摘要:从 System.Collections.Generic.List`1 中移除一定范围的元素。

参数说明:

  • index:要移除的元素范围的从零开始的起始索引。
  • count:要移除的元素数。

简单理解:移除指定下标的元素。

代码示例:

List<int> list = new List<int>() {11, 12, 13};

list.RemoveRange(1,2);

foreach (var item in list)
{
    Console.WriteLine(item);
}

--->
11

public void Reverse(int index, int count);

官方摘要:将指定范围中元素的顺序反转。

参数说明:

  • index:要反转的范围的从零开始的起始索引。
  • count:要反转的范围内的元素数。

简单理解:反转指定范围的元素。

代码示例:(其他重载,不列出)

List<int> list = new List<int>() {11, 12, 13};

list.Reverse(1,2);

foreach (var item in list)
{
    Console.WriteLine(item);
}

--->
11
13
12

public void Sort();

官方摘要:用默认比较器对整个 System.Collections.Generic.List`1 中的元素进行排序。

简单理解:为元素排序。

代码示例:(其他重载,不列出)

List<int> list = new List<int>() {13, 11, 12};

list.Sort();

foreach (var item in list)
{
    Console.WriteLine(item);
}

--->
11
12
13

public T[] ToArray();

官方摘要:将 System.Collections.Generic.List`1 的元素复制到新数组中。

返回结果:一个包含 System.Collections.Generic.List`1 的元素副本的数组。

简单理解:将元素复制到新的数组。

代码示例:

List<int> list = new List<int>() {13, 11, 12};

int[]arr = list.ToArray();

foreach (var item in arr)
{
    Console.WriteLine(item);
}

--->
13
11
12

public void TrimExcess();

官方摘要:将容量设置为 System.Collections.Generic.List`1 中元素的实际数目(如果该数目小于某个阈值)。

简单理解:将容量设置为元素的实际数目。

代码示例:

List<int> list = new List<int>(8) {13, 11, 12};

Console.WriteLine(list.Count);
Console.WriteLine(list.Capacity);

list.TrimExcess();

Console.WriteLine(list.Count);
Console.WriteLine(list.Capacity);

--->
3
8
3
3

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值