对于C#初学者来说,掌握这些数组方法非常重要。我来用循序渐进的方式详细解释每个方法:
1. Array.Copy() 和 CopyTo() - 数组复制
Array.Copy() - 静态方法
// 基本用法 int[] source = { 1, 2, 3, 4, 5 }; int[] destination = new int[5]; Array.Copy(source, destination, 3); // 复制前3个元素 Console.WriteLine(string.Join(", ", destination)); // 1, 2, 3, 0, 0 // 指定起始位置 int[] dest2 = new int[6]; Array.Copy(source, 1, dest2, 2, 3); // 从source[1]开始复制3个到dest2[2] Console.WriteLine(string.Join(", ", dest2)); // 0, 0, 2, 3, 4, 0
CopyTo() - 实例方法
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);
2. Exists() - 检查元素是否存在
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
3. GetValue() 和 SetValue() - 动态访问元素
GetValue() - 获取元素值
// 一维数组 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
SetValue() - 设置元素值
// 一维数组 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
4. GetLength() - 获取指定维度长度
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
5. Reverse() - 数组反转
// 反转整个数组 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
6. Sort() - 数组排序
// 基本排序 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
7. 综合实战练习
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)}"); } }
8. 记忆技巧表格
9. 学习建议
循序渐进:先从简单的一维数组开始练习
多写代码:每个方法都亲手敲一遍,观察结果
结合实际:用真实数据(成绩、姓名等)练习
调试观察:使用调试器查看数组变化
组合使用:尝试多个方法组合解决复杂问题
这样学习,你就能牢固掌握这些重要的数组方法了!