Generic collection class List<T> of C# data structure

Abstract: Talking about the concept and usage of the generic collection class List<T>.

Programming language: C#

Programming environment: Visual Studio 2019

Table of contents

concept

List initialization

Common properties of List

Common methods of List

Additions and deletions

lookup class

other

List traversal

summary

each message


concept

        The List<T> generic collection class is located in the System.Collections.Generic namespace, and a strongly typed collection can be realized by encapsulating objects. T is the type to be used, which can be a simple type (int, string, bool, etc.), a user-defined type, or a structure. Unlike ordinary arrays, generic collections can automatically and dynamically adjust the size of the collection as needed. List<T> is flexible and widely used in C# programming, and it is one of the data structures that must be mastered.

List<T> initialization

        Taking simple types and user-defined types as examples, the initialization of the generic collection List<T> is described. User-defined types are as follows:

 //用户自定义类型Students(学生)
    public class Students
    {
        public string Name;//学生姓名
        public int Score;//学生成绩
    }

        The first is to create an empty collection:

List<int> intlist = new List<int>();//创建普通类型int泛型空集合
List<Students> students = new List<Students>();//创建用户自定义类型Students泛型空集合

        How to assign the initial value? Just like initializing an array, insert curly braces {} after the parentheses of the new statement, and enter the corresponding initial value to assign an initial value to List<T>, as follows:

List<int> intList1 = new List<int>() { 0, 1, 2, 3, 5 };//创建带有初值的普通类型int泛型集合
List<Students> students1 = new List<Students>()
{
    new Students{Name="张三",Score=90},
    new Students{Name="李四",Score=91}
};//创建带有初值的用户自定义类型Students泛型集合

Common properties of List<T>

  • Capacity: Used to get or set (readable and writable) the capacity of the current List. This value will automatically grow when the number exceeds capacity. (Note: So one of the advantages of List<T> is that it will not exceed the limit. At the same time, the disadvantage is that it is easy to allocate redundant capacity. You can reduce the capacity by setting this value)
  • Count: Gets (read-only) the number of elements in the collection.
  • this[index]: Gets or sets (readable and writable) the element at the index value index. (Note: Same as array index access method)

Common methods of List<T>

Additions and deletions

  • Add(item): Add a single object of the same kind to the end of the collection, as follows:
List<Students> students2 = new List<Students>();//创建用户自定义类型Students泛型空集合
Students student3 = new Students();//新建一名学生对象
student3.Name = "王麻子";//赋姓名
student3.Score = 94;//赋分数
students2.Add(student3);//将其添加至集合结尾
  • AddRange(items): Add a collection of similar objects to the end of the collection, the usage is as follows: (Note: The collection of similar objects added can not only be a List<T> collection, but also an array)
List<int> intList2 = new List<int>() { 4 };//创建带有初值的普通类型int泛型集合
List<int> intList3 = new List<int>() { 0, 1, 2, 3, 5 };//创建带有初值的普通类型int泛型集合
int[] intArray1 = new int[3] { 3, 6, 9 };//创建带有初值的int型数组
intList2.AddRange(intList3);//将集合intList3添加至intList2结尾处
intList2.AddRange(intArray1);//将数组intArray1添加至intList2结尾处
  • Insert(index, item): Insert a single object of the same type at the specified index index.
  • InsertRange(index,items): Insert a collection of similar objects into the specified index index. (Note: The added collection of similar objects can not only be a List<T> collection, but also an array)
  • Remove(item): Remove the first matching item of the object item in the collection. The matching order is from small to large. If the matching is successful, the object is removed and true is returned. If the matching is unsuccessful, there is no element equal to item in the collection. , returns false. (Note: It is worth noting that if there are multiple such items in the collection, only the one with the smallest index number will be removed)
List<int> intList4 = new List<int>() { 0, 1, 2, 3, 0 };//创建带有初值的普通类型int泛型集合
intList4.Remove(0);//执行此语句将会移除集合intList4的索引号等于0处的0
  • RemoveAt(index): Removes the element corresponding to the index index in the collection.
  • RemoveRange(index,count): Counting from the collection index index, delete the count elements behind it. (Note: Contains the element itself corresponding to the index index)
List<int> intList5 = new List<int>() { 0, 1, 2, 3, 0 };//创建带有初值的普通类型int泛型集合
intList5.RemoveRange(1, 2);//执行此语句intList5变为{0,3,0}
  • Clear(): Removes all elements in the collection. (Note: The corresponding capacity will also be automatically cleared)

lookup class

  • Contains(item): Determine whether the element item is in the collection, return true if it exists, and return false if it does not exist, which is used for conditional judgment.
  • IndexOf(item): Returns the index number of the first matching item of the object item in the collection, the matching order is from small to large, and -1 is returned if the matching fails.
  • LastIndexOf: Returns the index number of the first matching item of the object item in the collection, the matching order is from large to small, and -1 is returned if the matching fails.

other

  • CopyTo(Array): Starting from the beginning of the target array, copies the entire collection into a compatible one-dimensional array. (Note: it will overwrite the original elements in the target array)
  • Reverse(): Reverse the order of the elements in the collection, that is, reverse order.
  • ToArray(): Copy the collection into a new array and return this array.
  • TrimExcess(): Set the collection capacity to the actual number of collection elements.

Traversal of List<T>

        For the traversal of List<T>, the for loop can be used, and the foreach loop can also be used. The latter uses more, and the basic syntax of traversal is

List<int> intList6 = new List<int>() { 0, 1, 2, 3, 0 };//创建带有初值的普通类型int泛型集合
foreach(int i in intList6)
{
    //foreach遍历,无需考虑遍历长度,遍历长度自适应,遍历规则只能从前往后,i为遍历对象
}
for(int i = 0; i < intList6.Count; i++)
{
    //for遍历,需考虑遍历长度,遍历规则可设置,intList6[i]为对象
}

summary

        This article briefly describes the initialization, properties, methods, and traversal of the List<T> generic collection. There are many other uses for it. For example, many methods (not listed in this article) use lambda expressions for operations, which is also very important. , the editor has not yet fully understood that piece, and will specifically talk about the usage of lambda expressions in the future.

each message

        The road ahead is confusing, but the road under your feet is real. There are only one step at a time. If you step on the road and move forward, you will never be disappointed.

 

Guess you like

Origin blog.csdn.net/lucgh/article/details/130276124