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 |
List |
Initializes a new instance of the List |
List |
Initializes a new instance of the List |
例子:
// 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 |
BinarySearch() | Uses a binary search algorithm to locate a specific element in the sorted List |
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 |
CopyTo() | Copies the List |
Equals(Object) | Determines whether the specified object is equal to the current object. |
Exists(Predicate |
Determines whether the List |
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 |
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 |
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 |
Insert(Int32, T) | Inserts an element into the List |
InsertRange(Int32, IEnumerable |
Inserts the elements of a collection into the List |
LastIndexOf() | Returns the zero-based index of the last occurrence of a value in the List |
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 |
Sort() | Sorts the elements or a portion of the elements in the List |
ToArray() | Copies the elements of the List |
ToString() | Returns a string that represents the current object. |
TrimExcess() | Sets the capacity to the actual number of elements in the List |
TrueForAll(Predicate |
Determines whether every element in the List |
范例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