C# 中的数组都是由 System.Array 类派生而来的引用对象,因此可以使用 Array 类中的各种属性或者方法对数组进行操作。例如,可以使用 Array 类的 Length 属性获取数组元素的长度,可以使用 Rank 属性获取数组的维数。
一、arr.Length属性
用途
返回数组中所有维度的元素总个数。
注意Length
属性适用于所有类型的数组(一维、多维、锯齿数组)。
语法
示例
// 一维数组
int[] numbers1D = { 1, 2, 3, 4, 5 };
Console.WriteLine($"一维数组Length: {numbers1D.Length}"); // 输出: 5
// 二维矩形数组
int[,] numbers2D = {
{ 1, 2, 3 },
{ 4, 5, 6 }
};
Console.WriteLine($"二维数组Length: {numbers2D.Length}"); // 输出: 6(2×3=6)
// 三维数组
int[,,] numbers3D = new int[2, 3, 4];
Console.WriteLine($"三维数组Length: {numbers3D.Length}"); // 输出: 24(2×3×4=24)
// 锯齿数组(只计算第一维)
int[][] jagged = {
new int[] {1, 2, 3},
new int[] {4, 5}
};
Console.WriteLine($"锯齿数组Length: {jagged.Length}"); // 输出: 2(只有2行)
二、arr.Rank属性
用途
返回数组的维度数量。 如:一维数组Rank=1,二维数组Rank=2,以此类推
语法
示例
int[] array1D = new int[5];
int[,] array2D = new int[3, 4];
int[,,] array3D = new int[2, 3, 4];
Console.WriteLine($"一维数组Rank: {array1D.Rank}"); // 输出: 1
Console.WriteLine($"二维数组Rank: {array2D.Rank}"); // 输出: 2
Console.WriteLine($"三维数组Rank: {array3D.Rank}"); // 输出: 3
三、Array.Copy()
用途
Array.Copy() 是用于复制数组元素的静态方法。
注意:有多个重载版本。
语法
// 方法签名
public static void Copy(Array sourceArray, Array destinationArray, int length);
public static void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length);
Array.Copy(源数组, 目标数组, 要复制的元素数量);
Array.Copy(源数组, 源数组起始索引, 目标数组, 目标数组起始索引, 要复制的元素数量);
示例
// 源数组
int[] source = { 1, 2, 3, 4, 5 };
// 目标数组(需要足够大的空间)
int[] destination = new int[5];
// 复制整个数组
Array.Copy(source, destination, source.Length);
Console.WriteLine("源数组: " + string.Join(", ", source));
Console.WriteLine("目标数组: " + string.Join(", ", destination));
// 输出: 源数组: 1, 2, 3, 4, 5
// 输出: 目标数组: 1, 2, 3, 4, 5
四、arr.CopyTo()
用途
CopyTo() 是数组的实例方法,用于将当前数组的元素复制到另一个数组中。
语法
// 方法签名
public void CopyTo(Array destinationArray, int destinationIndex);
public void CopyTo(Array destinationArray, long destinationIndex);
示例
int[] source = { 1, 2, 3, 4, 5 };
int[] destination = new int[7];
source.CopyTo(destination, 1); // 从destination[1]开始复制
Console.WriteLine(string.Join(", ", destination)); // 0, 1, 2, 3, 4, 5, 0
两种方法的区别
int[] arr1 = { 1, 2, 3 };
int[] arr2 = new int[3];
// 静态方法 - 需要指定源数组和目标数组
Array.Copy(arr1, arr2, arr1.Length);
// 实例方法 - 从当前数组复制到目标数组
arr1.CopyTo(arr2, 0);
五、 Array.Exists()
用途
用于检查数组中是否存在满足指定条件的元素的方法。
返回值
返回一个布尔值,表示是否至少有一个元素满足条件。
语法
// 方法签名
public static bool Exists<T>(T[] array, Predicate<T> match);
示例
string[] fruits = { "apple", "banana", "orange", "grape" };
// 检查是否存在包含"a"的水果
bool exists = Array.Exists(fruits, fruit => fruit.Contains("a"));
Console.WriteLine($"是否存在包含'a'的水果: {exists}"); // True
// 使用委托的完整写法
bool hasBanana = Array.Exists(fruits, delegate(string fruit) {
return fruit == "banana";
});
Console.WriteLine($"是否有香蕉: {hasBanana}"); // True
// 检查数字是否大于10
int[] numbers = { 5, 8, 12, 3, 15 };
bool hasBigNumber = Array.Exists(numbers, n => n > 10);
Console.WriteLine($"是否有大于10的数: {hasBigNumber}"); // True
六、arr.GetValue()
用途
GetValue() 用于获取数组中指定索引位置的元素值。它特别适用于处理未知类型的数组或多维数组。
注意:GetValue() 有多个重载版本,支持不同维度的数组。
语法
// 一维数组
public object GetValue(int index)
// 二维数组
public object GetValue(int index1, int index2)
// 三维数组
public object GetValue(int index1, int index2, int index3)
// 多维数组(使用参数数组)
public object GetValue(params int[] indices)
示例
// 一维数组
string[] names = { "Alice", "Bob", "Charlie" };
object name = names.GetValue(1); // 获取索引1的元素
Console.WriteLine(name); // Bob
// 多维数组
int[,] matrix = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
object value = matrix.GetValue(1, 0); // 获取[1,0]位置的元素
Console.WriteLine(value); // 3
/************************/
int[] numbers = { 10, 20, 30, 40, 50 };
// 使用 GetValue() 获取元素
object value1 = numbers.GetValue(0);
object value2 = numbers.GetValue(2);
object value3 = numbers.GetValue(4);
Console.WriteLine($"索引0的值: {value1} (类型: {value1.GetType()})");
Console.WriteLine($"索引2的值: {value2} (类型: {value2.GetType()})");
Console.WriteLine($"索引4的值: {value3} (类型: {value3.GetType()})");
// 需要转换为具体类型使用
int number = (int)numbers.GetValue(1);
Console.WriteLine($"转换后的值: {number * 2}");
七、arr.SetValue()
用途
SetValue() 是 System.Array 类的方法,用来 给数组中指定位置的元素赋值。
语法
// 给一维数组指定索引位置赋值
void SetValue(object value, int index);
// 给多维数组指定索引位置赋值
void SetValue(object value, int index1, int index2);
void SetValue(object value, int index1, int index2, int index3);
// 通用多维数组索引
void SetValue(object value, params int[] indices);
示例
// 一维数组
int[] numbers = new int[3];
numbers.SetValue(100, 0); // 设置numbers[0] = 100
numbers.SetValue(200, 1); // 设置numbers[1] = 200
Console.WriteLine(string.Join(", ", numbers)); // 100, 200, 0
// 多维数组
string[,] grid = new string[2, 2];
grid.SetValue("top-left", 0, 0);
grid.SetValue("top-right", 0, 1);
grid.SetValue("bottom-left", 1, 0);
Console.WriteLine(grid[1, 0]); // bottom-left
/********************/
int[] arr = new int[5];
// 使用 SetValue 给数组赋值
arr.SetValue(10, 0); // 相当于 arr[0] = 10;
arr.SetValue(20, 1); // 相当于 arr[1] = 20;
arr.SetValue(30, 2);
// 打印结果
foreach (int num in arr)
{
Console.WriteLine(num); 10 20 30 0 0
}
八、arr.GetLength()
用途
获取指定维度长度
返回值
返回数组在 指定维度上的长度(即该维度元素的数量)。
语法
GetLength(int dimension)
//dimension 参数是维度的索引(从 0 开始)。
示例
int[,] scores = {
{ 85, 90, 78 },
{ 92, 88, 95 },
{ 76, 82, 91 }
};
Console.WriteLine($"行数: {scores.GetLength(0)}"); // 3
Console.WriteLine($"列数: {scores.GetLength(1)}"); // 3
// 三维数组示例
int[,,] cube = new int[2, 3, 4];
Console.WriteLine($"第一维: {cube.GetLength(0)}"); // 2
Console.WriteLine($"第二维: {cube.GetLength(1)}"); // 3
Console.WriteLine($"第三维: {cube.GetLength(2)}"); // 4
九、Array.Reverse()
用途
Array.Reverse() 用于 反转数组元素的顺序。
注意:两种常用重载
语法
// 1. 反转整个数组
public static void Reverse(Array array);
// 2. 反转数组的某一部分
public static void Reverse(Array array, int index, int length);
示例
// 反转整个数组
int[] numbers = { 1, 2, 3, 4, 5 };
Array.Reverse(numbers);
Console.WriteLine(string.Join(", ", numbers)); // 5, 4, 3, 2, 1
// 反转部分数组
int[] numbers2 = { 1, 2, 3, 4, 5, 6 };
Array.Reverse(numbers2, 1, 3); // 从索引1开始反转3个元素
Console.WriteLine(string.Join(", ", numbers2)); // 1, 4, 3, 2, 5, 6
// 字符串数组反转
string[] words = { "hello", "world", "csharp" };
Array.Reverse(words);
Console.WriteLine(string.Join(" ", words)); // csharp world hello
十、Array.Sort()
用途
用于 对数组元素进行排序。 默认是 升序排列(从小到大),也可以自定义排序规则
语法
// 1. 对整个一维数组排序
public static void Sort(Array array);
// 2. 对数组的某一部分排序
public static void Sort(Array array, int index, int length);
// 3. 使用 IComparer 自定义排序规则
public static void Sort(Array array, IComparer comparer);
public static void Sort(Array array, int index, int length, IComparer comparer);
// 4. 键值对排序(两个数组同步排序)
public static void Sort(Array keys, Array items);
public static void Sort(Array keys, Array items, int index, int length);
示例
// 基本排序
int[] numbers = { 5, 2, 8, 1, 9 };
Array.Sort(numbers);
Console.WriteLine(string.Join(", ", numbers)); // 1, 2, 5, 8, 9
// 字符串排序
string[] fruits = { "orange", "apple", "banana", "grape" };
Array.Sort(fruits);
Console.WriteLine(string.Join(", ", fruits)); // apple, banana, grape, orange
// 部分排序
int[] numbers2 = { 5, 2, 8, 1, 9, 3, 7 };
Array.Sort(numbers2, 1, 4); // 从索引1开始排序4个元素
Console.WriteLine(string.Join(", ", numbers2)); // 5, 1, 2, 8, 9, 3, 7
// 自定义排序(降序)
int[] numbers3 = { 5, 2, 8, 1, 9 };
Array.Sort(numbers3, (a, b) => b.CompareTo(a)); // 降序排序
Console.WriteLine(string.Join(", ", numbers3)); // 9, 8, 5, 2, 1
int[] arr = { 5, 3, 8, 1, 4 };
Array.Sort(arr); // 默认升序
Console.WriteLine(string.Join(", ", arr));
int[] arr = { 9, 7, 5, 3, 1 };
// 从索引 1 开始,排序 3 个元素
Array.Sort(arr, 1, 3);
Console.WriteLine(string.Join(", ", arr));
int[] arr = { 5, 2, 8, 1 };
Array.Sort(arr); // 先升序
Array.Reverse(arr); // 再反转
Console.WriteLine(string.Join(", ", arr));
int[] arr = { 5, 2, 8, 1 };
Array.Sort(arr, new DescComparer());
Console.WriteLine(string.Join(", ", arr));
综合实战练习
class ArrayMethodsPractice
{
static void Main()
{
// 创建测试数组
int[] scores = { 85, 92, 78, 65, 95, 88, 72 };
Console.WriteLine("原始数组: " + string.Join(", ", scores));
// 1. 复制数组
int[] copiedScores = new int[scores.Length];
Array.Copy(scores, copiedScores, scores.Length);
Console.WriteLine("复制后: " + string.Join(", ", copiedScores));
// 2. 检查是否存在高分
bool hasHighScore = Array.Exists(scores, score => score >= 90);
Console.WriteLine($"是否有90分以上: {hasHighScore}");
// 3. 排序
Array.Sort(scores);
Console.WriteLine("排序后: " + string.Join(", ", scores));
// 4. 反转
Array.Reverse(scores);
Console.WriteLine("反转后: " + string.Join(", ", scores));
// 5. 使用GetValue/SetValue
object firstScore = Array.GetValue(scores, 0);
Console.WriteLine($"第一个分数: {firstScore}");
Array.SetValue(100, scores, 0);
Console.WriteLine("修改后: " + string.Join(", ", scores));
// 6. 多维数组操作
int[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 } };
Console.WriteLine($"数组维度: {matrix.Rank}");
Console.WriteLine($"行数: {matrix.GetLength(0)}");
Console.WriteLine($"列数: {matrix.GetLength(1)}");
}
}记忆技巧表格
/