List<T>
是一个类,它包含相同数据类型的多个对象,可以使用索引进行访问。例如:
// list containing integer values
List<int> number = new List<int>() { 1, 2, 3 };
这里,number
是一个包含整数值(**1**、**2** 和 **3**)的 List
。
创建 List
要在 C# 中创建 List<T>
,我们需要使用 System.Collections.Generic
命名空间。以下是创建 List<T>
的方法。例如:
using System;
using System.Collections.Generic;
class Program
{
public static void Main()
{
// create a list named subjects that contain 2 elements
List<string> subjects = new List<string>() { "English", "Math" };
}
}
访问 List 元素
我们可以使用索引表示法 []
来访问 List
。例如:
using System;
using System.Collections.Generic;
class Program
{
public static void Main()
{
// create a list
List<string> languages = new List<string>() { "Python", "Java" };
// access the first and second elements of languages list
Console.WriteLine("The first element of the list is " + languages[0]);
Console.WriteLine("The second element of the list is " + languages[1]);
}
}
输出
The first element of the list is Python The second element of the list is Java
由于列表的索引从 **0** 开始
language[0]
- 访问第一个元素language[5]
- 访问第四个元素
遍历 List
在 C# 中,我们也可以使用 for
循环遍历 List<T>
的每个元素。例如:
using System;
using System.Collections.Generic;
class Program
{
public static void Main()
{
// create a list
List<string> albums = new List<string>() { "Red", "Midnight", "Reputation" };
// iterate through the albums list
for (int i = 0; i < albums.Count; i++)
Console.WriteLine(albums[i]);
}
}
输出
Red Midnight Reputation
在上面的示例中,我们使用 for
循环遍历了 albums
列表。
注意: Count
属性返回列表中的总元素数。
List 的基本操作
List<T>
类提供了各种方法来对 List
执行不同的操作。在本教程中,我们将介绍一些常用的 List
操作。
- 添加元素
- 插入元素
- 移除元素
让我们详细讨论每项操作。
向 List 添加元素
要向 List
添加单个元素,我们使用 List<T>
类的 Add()
方法。例如:
using System;
using System.Collections.Generic;
class Program
{
public static void Main()
{
// create a list
List<string> country = new List<string>() { "Russia" };
//add "USA" to the country list
country.Add("USA");
// add "Japan" to the country list
country.Add("Japan");
// iterate through the country list
for (int i = 0; i < country.Count; i++)
Console.WriteLine(country[i]);
}
}
输出
Russia USA Japan
在上面的示例中,我们首先创建了一个包含 "Russia"
的 country
列表。
然后,我们使用 Add()
方法将 "USA"
和 "Japan"
添加到列表中。
在 List 中插入元素
要将元素插入到 List
的指定索引位置,我们使用 List<T>
类的 Insert()
方法。例如:
using System;
using System.Collections.Generic;
class Program
{
public static void Main()
{
// create a list
List<string> languages = new List<string>() { "Python", "Java", "C" };
// insert "JavaScript" at index 2
languages.Insert(2, "JavaScript");
// display element at index 2
Console.WriteLine(languages[2]);
}
}
输出
JavaScript
在上面的例子中:
languages.Insert(2, "JavaScript")
将"JavaScript"
插入到第 2 个索引位置。
从 List 中移除元素
我们可以使用 **2** 种方法从 List<T>
中删除一个或多个项。
Remove()
- 移除列表中给定元素的第一个匹配项。RemoveAt()
- 移除列表中指定位置的元素。
让我们通过两个方法来看示例。
示例:Remove() 方法
using System;
using System.Collections.Generic;
class Program
{
public static void Main()
{
var car = new List<string>() { "BMW", "Tesla", "Suzuki", "Tesla" };
// remove the first occurence of "Tesla" from the list
car.Remove("Tesla");
// remove the first occurrence of "Suzuki"
car.Remove("Suzuki");
// print the updated list after removing
for (int i = 0; i < car.Count; i++)
{
Console.WriteLine(car[i]);
}
}
}
输出
BMW Tesla
这里,
car.Remove("Tesla")
- 移除"Tesla"
的第一个匹配项。car.Remove("Suzuki)
- 移除"Suzuki"
的第一个匹配项。
我们可以看到,
- 原始列表:
{ "BMW", "Tesla", "Suzuki", "Tesla" }
- 修改后的列表:
{"BMW", "Tesla"}
示例:RemoveAt() 方法
using System;
using System.Collections.Generic;
class Program
{
public static void Main()
{
var car = new List<string>() { "BMW", "Tesla", "Suzuki", "Tesla" };
// remove the element present at the 2nd index position
car.RemoveAt(2);
// print the updated list after removing the element
for (int i = 0; i < car.Count; i++)
{
Console.WriteLine(car[i]);
}
}
}
输出
BMW Tesla Tesla
在上面的示例中,我们使用 RemoveAt()
方法移除了 List<T>
的元素。
这里,car.RemoveAt(2)
从列表中移除了 "Suzuki"
。
常见问题
我们也可以使用 var
关键字创建 List。例如:
using System;
using System.Collections.Generic;
class Program
{
public static void Main()
{
// create a list named color
var color = new List<string>() {
"Red",
"Blue",
"Pink"
};
Console.WriteLine(color[2]);
}
}
输出
Pink