首页 > 编程笔记 > C#笔记 阅读:44

C# Array数组类的用法(非常详细,附带实例)

C# 中,数组其实是 System 命名空间中的 Array 类衍生而来的对象,因此可以将 Array 类想成是支持数组操作的类,这个类提供了许多方法,可以更便于我们操作数组。

C# Array类的属性

常见 Array 类的属性如下:
例如,列出数组的元素个数和维度:
int[] num1 = { 1, 2, 3, 4, 5 };
int[,] num2 = new int[,] { { 6, 66 },
                          { 2, 22 },
                          { 5, 55 }};
Console.WriteLine($"num1 的元素个数 = {num1.Length}");
Console.WriteLine($"num1 的数组维度 = {num1.Rank}");
Console.WriteLine($"num2 的元素个数 = {num2.Length}");
Console.WriteLine($"num2 的数组维度 = {num2.Rank}");
执行结果为:

num1 的元素个数 = 5
num1 的数组维度 = 1
num2 的元素个数 = 6
num2 的数组维度 = 2

C# Array类常用的方法

下表罗列了 Array 类常用的方法:

方法 说明
Clear() 将数组内容清除,也就是设为默认值。
Copy() 将某数组范围的元素复制到另一数组内。
GetLength() 取得指定维度的元素个数。
GetLowerBound() 取得指定维度第一个元素的索引。
GetUpperBound() 取得指定维度最后一个元素的索引。
GetValue() 取得指定索引的值。
SetValue() 设定指定索引的内容。
IndexOf() 回传指定值在数组第一次出现时的索引。
Reverse() 将数组元素位置反转。
Sort() 将一维数组元素排序。
BinarySearch() 在已经排序的数组中,执行二元搜寻法。

1) C# Clear()清除数组内容

所谓的清除(clear)是指将数组内容改为声明时数据类型默认内容,此方法的语法如下:
Array.Clear(array, index, length);
array 是要清除的数组,index 是要清除的起始索引,length 是要清除的元素数量,若是省略 index 和 length 则是清除所有数组元素。

例如:
int[] num = { 1, 2, 3, 4, 5 };
Console.WriteLine("使用Clear前的数组内容");
foreach (int n in num)
    Console.Write($"{n} ");
Console.WriteLine();
Array.Clear(num);
Console.WriteLine("使用Clear后的数组内容");
foreach (int n in num)
    Console.Write($"{n} ");
执行结果为:

使用Clear前的数组内容
1 2 3 4 5
使用Clear后的数组内容
0 0 0 0 0

2) C# Copy()方法

Copy()方法用于将某数组范围的元素复制到另一数组内,它的语法如下:
Array.Copy(srcArray, dstArray, length);
其将 srcArray 数组的第一个元素复制到 dstArray 数组的第一个元素,复制长度则由 length 设定。

例如,将 src 数组前 3 个元素复制到 dst 数组:
int[] src = new int[] { 1, 2, 3, 4, 5 };
int[] dst = new int[5];
Array.Copy(src, dst, 3);
foreach (int value in dst)
    Console.Write($"{value} ");
执行结果为:

1 2 3 0 0


再例如,将 src 数组的后 3 个元素复制到 dst 数组并从索引 1 开始:
int[] src = new int[] { 1, 2, 3, 4, 5 };
int[] dst = new int[5];
Array.Copy(src, 2, dst, 1, 3);
foreach (int value in dst)
    Console.Write($"{value} ");
执行结果为:

0 3 4 5 0

3) C# GetLength()/GetLowerBound()/GetUpperBound()方法

GetLength() 和 GetUpperBound() 常被用在测试索引是否超出界线,这 3 个方法的语法如下:
上述 arr 是 Array 对象,参数 dim 代表指定维度,0 代表第 1 维度,1 代表第 2 维度,以此类推。

例如,使用 GetLength()/GetLowerbound()/GetUpperBound() 方法获得二维的 row 和 column 数量,以及第 2 维度数组的元素数量与索引数据:
int[,] arr = new int[,] { { 6, 66 },
                         { 2, 22 },
                         { 5, 55 }};

Console.WriteLine($"row 数量:{arr.GetLength(0)}");
Console.WriteLine($"column 数量:{arr.GetLength(1)}");
Console.WriteLine("第 2 维度资料");
Console.WriteLine($"元素数量:{arr.GetLength(1)}");
Console.WriteLine($"第1个索引:{arr.GetLowerBound(1)}");
Console.WriteLine($"最后一个索引:{arr.GetUpperBound(1)}");
执行结果为:

row 数量:3
column 数量:2
第 2 维度资料
元素数量:2
第1个索引:0
最后一个索引:1

4) C# SetValue()/GetValue()方法

SetValue() 设定数组指定索引内容,GetValue() 是取得数组特定索引内容,语法如下:
上述 arr 是 Array 对象。

例如:
int[] arr = new int[10];
int index = 5;    // 索引
int value = 8;    // 设置值
Console.WriteLine("原始数组内容");
foreach (int i in arr)
    Console.Write($"{i} ");
Console.WriteLine("\n目前数组内容");
arr.SetValue(value, index);
Console.WriteLine("\n索引 {index} 内容 = {arr.GetValue(index)}");
执行结果为:

原始数组内容
0 0 0 0 0 0 0 0 0 0
目前数组内容
0 0 0 0 0 8 0 0 0 0
索引 5 内容 = 8

5) C# IndexOf()方法

这个方法可以回传指定值的索引,其语法如下:
Array.IndexOf(array, value)
上述语法回传 array 数组内 value 值第一次出现的索引。

例如,使用 IndexOf() 方法找出数值 8 第一次索引:
int[] array = { 3, 8, 9, 8, 7 };
int value = 8;
int idx = Array.IndexOf(array, value);
Console.WriteLine($"数据 {value} 第 1 次出现索引是 {idx}");
执行结果为:

数据 8 第 1 次出现索引是 1

6) C# Reverse()/Sort()方法

Array.Reverse() 可以反转数组,Array.Sort() 则是将数组排序。

例如:
int[] array = { 3, 8, 9, 2, 7 };
Console.WriteLine("原始数组");
foreach (int i in array)
    Console.Write($"{i} ");
Array.Reverse(array);
Console.WriteLine("\n反转数组");
foreach (int i in array)
    Console.Write($"{i} ");
Array.Sort(array);
Console.WriteLine("\n数组排序");
foreach (int i in array)
    Console.Write($"{i} ");
执行结果为:

原始数组
3 8 9 2 7
反转数组
7 2 9 8 3
数组排序
2 3 7 8 9

7) C# BinarySearch()方法

在已经排序的数组中,执行二元搜寻法,二分搜寻法是算法领域很重要的搜寻法,在搜寻前需将数据排序,可以让搜寻的效率变高,假设有 n 笔数据要搜寻,搜寻的时间复杂度是 O(log n)。

BinarySearch() 方法的基础语法如下:
int idx = Array.BinarySearch(arr, value);
如果找到目标则回传索引值,如果找不到目标则回传负值。

例如:
int[] data = { 90, 40, 30, 20, 50 };
Array.Sort(data);
Console.WriteLine("数组内容如下 ...");
foreach (int i in data)
    Console.WriteLine(i);
Console.Write("元素 50 索引:" + Array.BinarySearch(data, 50));
执行结果为:

数组内容如下 ...
20
30
40
50
90
元素 50 索引:3

相关文章