📜  C#|清单类别

📅  最后修改于: 2021-05-29 17:13:39             🧑  作者: Mango

List 表示可以通过索引访问的对象列表。它位于System.Collection.Generic命名空间下。 List类可用于创建不同类型的集合,例如整数,字符串等。List 类还提供了搜索,排序和操作列表的方法。

特征:

  • 它与数组不同。可以动态调整List 的大小,不能调整数组的大小
  • List 类可以接受null作为引用类型的有效值,并且还允许重复的元素。
  • 如果Count等于Capacity,则List的容量通过重新分配内部数组自动增加。在添加新元素之前,现有元素将被复制到新数组。
  • 通过实现IList 泛型接口,List 类与ArrayList类的泛型等效项。
  • 此类可以使用相等和排序比较器。
  • 默认情况下,List 类未排序,并且元素从零开始进行索引访问。
  • 对于非常大的List 对象,通过在运行时环境中将配置元素的enabled属性设置为true,可以将64位系统上的最大容量增加到20亿个元素

建设者

Constructor Description
List() Initializes a new instance of the List class that is empty and has the default initial capacity.
List(IEnumerable) Initializes a new instance of the List class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.
List(Int32) Initializes a new instance of the List class that is empty and has the specified initial capacity.

例子:

// C# program to create a List
using System;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a List of integers
        List firstlist = new List();
  
        // displaying the number
        // of elements of List
        Console.WriteLine(firstlist.Count);
    }
}

输出:

0

特性

Property Description
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.

例子:

// C# program to illustrate the
// Capacity Property of List
using System;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a List of integers
        // Here we are not setting
        // Capacity explicitly
        List firstlist = new List();
  
        // adding elements in firstlist
        firstlist.Add(1);
        firstlist.Add(2);
        firstlist.Add(3);
        firstlist.Add(4);
  
        // Printing the Capacity of firstlist
        Console.WriteLine("Capacity Is: " + firstlist.Capacity);
  
        // Printing the Count of firstlist
        Console.WriteLine("Count Is: " + firstlist.Count);
  
        // Adding some more
        // elements in firstlist
        firstlist.Add(5);
        firstlist.Add(6);
  
        // Printing the Capacity of firstlist
        // It will give output 8 as internally
        // List is resized
        Console.WriteLine("Capacity Is: " + firstlist.Capacity);
  
        // Printing the Count of firstlist
        Console.WriteLine("Count Is: " + firstlist.Count);
    }
}

输出:

Capacity Is: 4
Count Is: 4
Capacity Is: 8
Count Is: 6

方法

Method Description
Add(T) Adds an object to the end of the List.
AddRange(IEnumerable) Adds the elements of the specified collection to the end of the List.
AsReadOnly() Returns a read-only ReadOnlyCollection wrapper for the current collection.
BinarySearch() Uses a binary search algorithm to locate a specific element in the sorted List or a portion of it.
Clear() Removes all elements from the List.
Contains(T) Determines whether an element is in the List.
ConvertAll(Converter) Converts the elements in the current List to another type, and returns a list containing the converted elements.
CopyTo() Copies the List or a portion of it to an array.
Equals(Object) Determines whether the specified object is equal to the current object.
Exists(Predicate) Determines whether the List contains elements that match the conditions defined by the specified predicate.
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() Searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the first occurrence within the List or a portion of it. This method returns -1 if an item that matches the conditions is not found.
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() Searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the last occurrence within the List or a portion of it.
ForEach(Action) Performs the specified action on each element of the List.
GetEnumerator() Returns an enumerator that iterates through the List.
GetHashCode() Serves as the default hash function.
GetRange(Int32, Int32) Creates a shallow copy of a range of elements in the source List.
GetType() Gets the Type of the current instance.
IndexOf() Returns the zero-based index of the first occurrence of a value in the List or in a portion of it.
Insert(Int32, T) Inserts an element into the List at the specified index.
InsertRange(Int32, IEnumerable) Inserts the elements of a collection into the List at the specified index.
LastIndexOf() Returns the zero-based index of the last occurrence of a value in the List or in a portion of it.
MemberwiseClone() Creates a shallow copy of the current Object.
Remove(T) Removes the first occurrence of a specific object from the List.
RemoveAll(Predicate) Removes all the elements that match the conditions defined by the specified predicate.
RemoveAt(Int32) Removes the element at the specified index of the List.
RemoveRange(Int32, Int32) Removes a range of elements from the List.
Reverse() Reverses the order of the elements in the List or a portion of it.
Sort() Sorts the elements or a portion of the elements in the List using either the specified or default IComparer implementation or a provided Comparison delegate to compare list elements.
ToArray() Copies the elements of the List to a new array.
ToString() Returns a string that represents the current object.
TrimExcess() Sets the capacity to the actual number of elements in the List, if that number is less than a threshold value.
TrueForAll(Predicate) Determines whether every element in the List matches the conditions defined by the specified predicate.

范例1:

// C# Program to check whether the
// element is present in the List
// or not
using System;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating an List of Integers
        List firstlist = new List();
  
        // Adding elements to List
        firstlist.Add(1);
        firstlist.Add(2);
        firstlist.Add(3);
        firstlist.Add(4);
        firstlist.Add(5);
        firstlist.Add(6);
        firstlist.Add(7);
  
        // Checking whether 4 is present
        // in List or not
        Console.Write(firstlist.Contains(4));
    }
}

输出:

True

范例2:

// C# Program to remove the element at
// the specified index of the List
using System;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating an List of Integers
        List firstlist = new List();
  
        // Adding elements to List
        firstlist.Add(17);
        firstlist.Add(19);
        firstlist.Add(21);
        firstlist.Add(9);
        firstlist.Add(75);
        firstlist.Add(19);
        firstlist.Add(73);
  
        Console.WriteLine("Elements Present in List:\n");
  
        int p = 0;
  
        // Displaying the elements of List
        foreach(int k in firstlist)
        {
            Console.Write("At Position {0}: ", p);
            Console.WriteLine(k);
            p++;
        }
  
        Console.WriteLine(" ");
  
        // removing the element at index 3
        Console.WriteLine("Removing the element at index 3\n");
  
        // 9 will remove from the List
        // and 75 will come at index 3
        firstlist.RemoveAt(3);
  
        int p1 = 0;
  
        // Displaying the elements of List
        foreach(int n in firstlist)
        {
            Console.Write("At Position {0}: ", p1);
            Console.WriteLine(n);
            p1++;
        }
    }
}

输出:

Elements Present in List:

At Position 0: 17
At Position 1: 19
At Position 2: 21
At Position 3: 9
At Position 4: 75
At Position 5: 19
At Position 6: 73
 
Removing the element at index 3

At Position 0: 17
At Position 1: 19
At Position 2: 21
At Position 3: 75
At Position 4: 19
At Position 5: 73

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.list-1?view=netframework-4.7.2