集合操作全解析:从基础数组到泛型集合
立即解锁
发布时间: 2025-08-20 01:13:16 订阅数: 1 


C#面向对象编程与Web服务开发
### 集合操作全解析:从基础数组到泛型集合
在编程的世界里,集合操作是一项非常重要的技能。我们将深入探讨常见的集合类,包括数组、ArrayList 以及泛型集合的使用方法和相关操作。
#### 常用集合类及接口
在开始具体的集合操作之前,先了解一些常用的集合类和它们实现的接口。
| 集合类 | 描述 |
| --- | --- |
| Array | 为支持强类型数组的语言实现提供基类 |
| ArrayList | 使用动态调整大小的数组表示弱类型对象列表 |
| SortedList | 表示按键排序的键/值对集合,可通过键和索引访问 |
| Queue | 表示先进先出 (FIFO) 的对象集合 |
| Stack | 表示后进先出 (LIFO) 的非泛型对象集合 |
| Hashtable | 表示基于键的哈希代码组织的键/值对集合 |
| CollectionBase | 为强类型集合提供抽象基类 |
| DictionaryBase | 为强类型键/值对集合提供抽象基类 |
这些集合类实现了一些重要的接口,如下表所示:
| 接口 | 描述 |
| --- | --- |
| ICollection | 定义所有非泛型集合的大小、枚举器和同步方法 |
| IComparer | 公开比较两个对象的方法 |
| IDictionary | 表示非泛型键/值对集合 |
| IDictionaryEnumerator | 枚举非泛型字典的元素 |
| IEnumerable | 公开枚举器,支持对非泛型集合的简单迭代 |
| IEnumerator | 支持对非泛型集合的简单迭代 |
| IList | 表示可通过索引单独访问的非泛型对象集合 |
#### 数组和 ArrayList 的使用
数组是计算机编程中最常见的数据结构之一,它可以存储相同数据类型的元素。数组的元素可以通过索引访问,索引是一个整数,表示元素在数组中的位置。
下面是一个表示一周七天的一维数组示例:
| 索引 | 值 |
| --- | --- |
| 0 | Sunday |
| 1 | Monday |
| 2 | Tuesday |
| 3 | Wednesday |
| 4 | Thursday |
| 5 | Friday |
| 6 | Saturday |
数组还可以是多维的,例如二维数组可以用来表示座位表。以下是一个二维数组的示例:
```mermaid
graph LR
classDef startend fill:#F5EBFF,stroke:#BE8FED,stroke-width:2px;
classDef process fill:#E5F6FF,stroke:#73A6FF,stroke-width:2px;
A([Row 0]):::startend --> B(Seat 0):::process
A --> C(Seat 1):::process
A --> D(Seat 2):::process
B --> E(Mary<br>(0,0)):::process
C --> F(Bob<br>(0,1)):::process
D --> G(Amy<br>(0,2)):::process
H([Row 1]):::startend --> I(Seat 0):::process
H --> J(Seat 1):::process
H --> K(Seat 2):::process
I --> L(Jim<br>(1,0)):::process
J --> M(Noah<br>(1,1)):::process
K --> N(Morgan<br>(1,2)):::process
O([Row 2]):::startend --> P(Seat 0):::process
O --> Q(Seat 1):::process
O --> R(Seat 2):::process
P --> S(Jane<br>(2,0)):::process
Q --> T(Cindy<br>(2,1)):::process
R --> U(Greg<br>(2,2)):::process
```
在声明数组类型时,可以使用方括号 `[]` 来指定数组。以下是一个声明并初始化一维整数数组的示例:
```csharp
int[] intArray = { 1, 2, 3, 4, 5 };
```
声明为数组后,可以使用 `Array` 类的属性和方法,例如查询数组的上下界、更新数组元素、复制数组元素等。以下是一个使用 `Array` 类静态方法的示例:
```csharp
int[] intArray = { 1, 2, 3, 4, 5 };
Console.WriteLine("Upper Bound");
Console.WriteLine(intArray.GetUpperBound(0));
Console.WriteLine("Array elements");
foreach (int item in intArray)
{
Console.WriteLine(item);
}
Array.Reverse(intArray);
Console.WriteLine("Array reversed");
foreach (int item in intArray)
{
Console.WriteLine(item);
}
Array.Clear(intArray, 2, 2);
Console.WriteLine("Elements 2 and 3 cleared");
foreach (int item in intArray)
{
Console.WriteLine(item);
}
intArray[4] = 9;
Console.WriteLine("Element 4 reset");
foreach (int item in intArray)
{
Console.WriteLine(item);
}
Console.ReadLine();
```
二维数组的声明和初始化方式如下:
```csharp
int[,] twoDArray = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
//Print the index and value of the elements
for (int i = 0; i <= twoDArray.GetUpperBound(0); i++)
{
for (int x = 0; x <= twoDArray.GetUpperBound(1); x++)
{
Console.WriteLine("Index = [{0},{1}] Value = {2}", i, x, twoDArray[i, x]);
}
}
```
在处理集合时,有时在运行时才知道需要包含的项数,这时 `ArrayList` 类就派上用场了。`ArrayList` 的容量会根据需要自动扩展,并且会自动进行内存重新分配和元素复制。以下是一个使用 `ArrayList` 的示例:
```csharp
ArrayList nameList = new ArrayList();
nameList.Add("Bob");
nameList.Add("Dan");
nameList.Add("Wendy");
Console.WriteLine("Original Capacity");
Console.WriteLine(nameList.Capacity);
Console.WriteLine("Original Values");
foreach (object name in nameList)
{
Console.WriteLine(name);
}
nameList.Insert(nameList.IndexOf("Dan"), "Cindy");
nameList.Insert(nameList.IndexOf("Wendy"), "Jim");
Console.WriteLine("New Capacity");
Console.WriteLine(nameList.Capacity);
Console.WriteLine("New Values");
foreach (object name in nameList)
{
Console.WriteLine(name);
}
```
虽然 `ArrayList` 使用起来比数组更方便,但它只能是一维的,并且特定类型的数组比 `ArrayList` 性能更好,因为 `ArrayList` 的元素类型是 `Object`,在添加和检索元素时需要进行类型转换。
#### 数组和 ArrayList 的操作实践
下面通过具体的操作步骤来实践数组和 ArrayList 的使用。
##### 创建和使用数组
1. 启动 Visual Studio,选择“文件” -> “新建” -> “项目”。
2. 选择控制台应用程序项目,将项目命名为 `activity9_1`。
3. 注意 `Main` 方法接受一个名为 `args` 的字符串数组作为输入参数,该数组包含在启动控制台应用程序时传递的命令行参数。
```csharp
static void Main(string[] args)
{
}
```
4. 在 `Main` 方法中添加以下代码,用于显示传递的命令行参数:
```csharp
Console.WriteLine(
```
0
0
复制全文
相关推荐









