C#学习笔记——List

C# List 用法简单介绍

概述:

      C#的List容器和C++的list容器类似,也是一种链式的泛型存储结构,可以按需要动态地增加容量的 大小。

命名空间: System.Collections.Generic

SerializableAttribute:

public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>  //T表示类型  

属性:

名字 描述
Capacity Gets or sets the total number of elements the internal data structure can hold without resizing.
Count Gets the number of elements contained in the List<>.
Item[Int32] Gets or sets the element at the specified index.

常用方法:

声明:

构造函数 说明
List<>() 声明一个空的List对象
List<>(IEnumerable<>) 实例化一个新的List对象,该对象包含了从参数的那个容器中复制过来的元素
List<>(Int32) 实例化一个空的对象,该对象有指定的容量

这里以string类型作为示例

List<string> strs = new List<string>();//声明空对象
string[] plants = { "水星", "金星", "地球", "火星", "木星", "土星", "天王星", "海王星 " };
List<string> sunSystem = new List<string>(plants);//用其他容器来实例化一个List

添加元素:

方法 说明
Add(T) 在List的尾部增加一个对象
AddRange(IEnumerable<>) 将一个容器的元素增加至List的尾部
Insert(Int32, index) 在特定的位置插入一个元素

接前面的代码

strs.Add("first element");
strs.Add("second element");
strs.AddRange(plants);
strs.Insert(3, "new element");

遍历List中的元素:

      遍历List中的元素有两种方法,第一种是用foreach来遍历,第二种是循环遍历。

foreach(string str in sunSystem)
{
    Console.Write(str + " ");
}
Console.WriteLine();
for(int i = 0; i < plants.Length; i++)
{
    Console.Write(plants[i] + ' ');
}

删除元素:

方法 说明
Remove(T) 删除第一个出现的元素“T”
RemoveAll(Predicate<>) 删除所有符合断言条件的元素
RemoveAt(index) 删除指定下标的元素
RemoveRange(index, count) 删除从index开始的count个元素

排序:

排序直接用Sort()方法即可,默认为升序排序。其他的排序方法还有:

方法 说明
Sort(Comparison<>) Sorts the elements in the entire List<> using the specified System.Comparison<>.
Sort(IComparer<>) Sorts the elements in the entire List using the specified comparer.
Sort(Int32, Int32, IComparer<>) Sorts the elements in a range of elements in List<> using the specified comparer.

另外还有两个反转函数,可合理利用:

方法 说明
Reverse() Reverses the order of the elements in the entire List<>.
Reverse(Int32, Int32) Reverses the order of the elements in the specified range.

查找:

方法 说明
Find(Predicate<>) Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire List<>.
FindAll(Predicate<>) Retrieves all the elements that match the conditions defined by the specified predicate.
FindIndex(Int32, Int32, Predicate<>) Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the List<> that starts at the specified index and contains the specified number of elements.
FindIndex(Int32, Predicate<>) Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the List<> that extends from the specified index to the last element.
FindIndex(Predicate<>) Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire List<>.
FindLast(Predicate<>) Searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire List<>.
FindLastIndex(Int32, Int32, Predicate<>) Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the range of elements in the List that contains the specified number of elements and ends at the specified index.
FindLastIndex(Int32, Predicate<>) Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the range of elements in the List<> that extends from the first element to the specified index.
FindLastIndex(Predicate<>) Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the entire List<>.

除此之外,还有三个BinarySearch()方法,具体就不再列举了。

其他常用方法:

方法 说明
Clear() 清空所有元素
Contains(T) 判定一个元素是否在List中
Exists(Predicate<>) 判定list中是否有符合条件的元素
ToArray() 将list中的元素复制到一个数组
TrueForAll(Predicate<>) 判定是否所有元素都符合条件

更多的关于List的方法可去Microsoft的文档中查找
Microsoft文档

你可能感兴趣的:(C#)