尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

常见的Array方法

常见的Array方法 Array是 C# 中所有数组的基类位于System命名空间提供了大量静态方法用于排序、查找、复制、转换等操作以及若干实例方法/属性。下面按功能分类逐一详解并附完整案例。 常用属性属性说明Length所有维度的元素总数intLongLength所有维度的元素总数long大数组用Rank数组的维度数IsFixedSize是否固定大小数组始终为 trueIsReadOnly是否只读数组始终为 false1. 排序与反转Array.Sort()— 升序排序int[] arr { 34, 72, 13, 44, 25, 30, 10 }; ​ Array.Sort(arr); Console.WriteLine(string.Join(, , arr)); // 输出: 10, 13, 25, 30, 34, 44, 72 ​ // 对指定范围排序从索引 2 开始排序 3 个元素 int[] arr2 { 5, 3, 8, 1, 9, 2 }; Array.Sort(arr2, 2, 3); // 对索引 2~4 的元素排序 Console.WriteLine(string.Join(, , arr2)); // 输出: 5, 3, 1, 8, 9, 2Array.Reverse()— 反转元素顺序int[] arr { 1, 2, 3, 4, 5 }; ​ Array.Reverse(arr); Console.WriteLine(string.Join(, , arr)); // 输出: 5, 4, 3, 2, 1 ​ // 反转指定范围 int[] arr2 { 1, 2, 3, 4, 5, 6 }; Array.Reverse(arr2, 1, 4); // 反转索引 1~4 的元素 Console.WriteLine(string.Join(, , arr2)); // 输出: 1, 5, 4, 3, 2, 62. 查找与搜索Array.IndexOf()— 查找首次出现的索引int[] arr { 10, 20, 30, 20, 40 }; ​ int index Array.IndexOf(arr, 20); Console.WriteLine(index); // 输出: 1首次出现的位置 ​ int notFound Array.IndexOf(arr, 99); Console.WriteLine(notFound); // 输出: -1未找到 ​ // 从指定索引开始查找 int index2 Array.IndexOf(arr, 20, 2); // 从索引 2 开始找 Console.WriteLine(index2); // 输出: 3Array.LastIndexOf()— 查找最后出现的索引int[] arr { 10, 20, 30, 20, 40 }; ​ int lastIndex Array.LastIndexOf(arr, 20); Console.WriteLine(lastIndex); // 输出: 3Array.BinarySearch()— 二分查找必须先排序int[] arr { 10, 20, 30, 40, 50 }; // 已排序 ​ int index Array.BinarySearch(arr, 30); Console.WriteLine(index); // 输出: 2 ​ int notFound Array.BinarySearch(arr, 35); Console.WriteLine(notFound); // 输出: 负数按位取反后是应插入的位置 // 实际值: ~(-4) 3表示应插入到索引 3⚠️ 使用BinarySearch前数组必须已排序否则结果不可预测。3. 条件查找泛型方法需配合 LambdaArray.Find()— 查找第一个满足条件的元素int[] ages { 25, 17, 30, 12, 40, 15 }; ​ int first Array.Find(ages, x x 18); Console.WriteLine(first); // 输出: 17 ​ // 查找第一个偶数 int firstEven Array.Find(ages, x x % 2 0); Console.WriteLine(firstEven); // 输出: 30Array.FindAll()— 查找所有满足条件的元素int[] ages { 25, 17, 30, 12, 40, 15 }; ​ int[] minors Array.FindAll(ages, x x 18); Console.WriteLine(string.Join(, , minors)); // 输出: 17, 12, 15Array.FindIndex()— 查找第一个满足条件的索引int[] ages { 25, 17, 30, 12, 40 }; ​ int index Array.FindIndex(ages, x x 18); Console.WriteLine(index); // 输出: 1 ​ int notFound Array.FindIndex(ages, x x 100); Console.WriteLine(notFound); // 输出: -1Array.FindLast()/Array.FindLastIndex()int[] ages { 25, 17, 30, 12, 40, 15 }; ​ int last Array.FindLast(ages, x x 18); Console.WriteLine(last); // 输出: 15 ​ int lastIndex Array.FindLastIndex(ages, x x 18); Console.WriteLine(lastIndex); // 输出: 54. 条件判断Array.Exists()— 是否存在满足条件的元素int[] arr { 2, 4, 6, 8 }; ​ bool hasOdd Array.Exists(arr, x x % 2 ! 0); Console.WriteLine(hasOdd); // 输出: False ​ bool hasLarge Array.Exists(arr, x x 5); Console.WriteLine(hasLarge); // 输出: TrueArray.TrueForAll()— 是否所有元素都满足条件int[] arr { 2, 4, 6, 8 }; ​ bool allEven Array.TrueForAll(arr, x x % 2 0); Console.WriteLine(allEven); // 输出: True ​ arr[3] 7; bool allEven2 Array.TrueForAll(arr, x x % 2 0); Console.WriteLine(allEven2); // 输出: False5. 遍历Array.ForEach()— 对每个元素执行操作int[] arr { 1, 2, 3, 4, 5 }; ​ Array.ForEach(arr, x Console.Write(x )); // 输出: 1 2 3 4 5 ​ Console.WriteLine(); ​ // 也可以用于修改外部状态 int sum 0; Array.ForEach(arr, x sum x); Console.WriteLine($总和: {sum}); // 输出: 总和: 156. 清空Array.Clear()— 将元素重置为默认值int[] arr { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; ​ // 从索引 2 开始清空 5 个元素置为 0 Array.Clear(arr, 2, 5); Console.WriteLine(string.Join(, , arr)); // 输出: 1, 2, 0, 0, 0, 0, 0, 8, 9 ​ // .NET 6 新增清空整个数组 int[] arr2 { 10, 20, 30 }; Array.Clear(arr2); Console.WriteLine(string.Join(, , arr2)); // 输出: 0, 0, 0 ​ // 引用类型会被置为 null string[] strs { a, b, c }; Array.Clear(strs, 0, 2); Console.WriteLine(string.Join(, , strs.Select(s s ?? null))); // 输出: null, null, c7. 复制Array.Copy()— 静态方法复制元素int[] source { 1, 2, 3, 4, 5 }; int[] dest new int[5]; ​ // 复制全部元素 Array.Copy(source, dest, source.Length); Console.WriteLine(string.Join(, , dest)); // 输出: 1, 2, 3, 4, 5 ​ // 从源数组索引 2 开始复制到目标数组索引 1复制 3 个元素 int[] source2 { 10, 20, 30, 40, 50 }; int[] dest2 new int[5]; Array.Copy(source2, 2, dest2, 1, 3); Console.WriteLine(string.Join(, , dest2)); // 输出: 0, 30, 40, 50, 0CopyTo()— 实例方法复制string[] source { 吴亦凡, 罗志祥, 李云迪 }; string[] dest new string[5]; ​ source.CopyTo(dest, 1); // 从目标数组索引 1 开始存放 Console.WriteLine(string.Join(, , dest.Select(s s ?? null))); // 输出: null, 吴亦凡, 罗志祥, 李云迪, nullClone()— 浅表副本int[] arr { 1, 2, 3 }; int[] cloned (int[])arr.Clone(); ​ cloned[0] 99; Console.WriteLine(arr[0]); // 输出: 1原数组不受影响 Console.WriteLine(cloned[0]); // 输出: 998. 调整大小Array.Resize()— 改变一维数组大小int[] arr { 1, 2, 3 }; ​ // 扩容 Array.Resize(ref arr, 5); Console.WriteLine(string.Join(, , arr)); // 输出: 1, 2, 3, 0, 0新增元素为默认值 ​ // 缩容 Array.Resize(ref arr, 2); Console.WriteLine(string.Join(, , arr)); // 输出: 1, 2⚠️Resize实际上是创建新数组并复制旧数据频繁调用会影响性能。9. 创建与维度信息Array.CreateInstance()— 动态创建数组// 创建长度为 5 的 int 数组 Array arr Array.CreateInstance(typeof(int), 5); arr.SetValue(10, 0); arr.SetValue(20, 1); Console.WriteLine(arr.GetValue(0)); // 输出: 10 ​ // 创建 2x3 的二维数组 Array arr2d Array.CreateInstance(typeof(string), 2, 3); arr2d.SetValue(A, 0, 0); arr2d.SetValue(B, 0, 1); Console.WriteLine(arr2d.GetValue(0, 0)); // 输出: A Console.WriteLine(arr2d.GetValue(0, 1)); // 输出: B维度相关方法int[,] matrix new int[3, 4]; ​ Console.WriteLine(matrix.Rank); // 输出: 2二维 Console.WriteLine(matrix.Length); // 输出: 123×4 Console.WriteLine(matrix.GetLength(0)); // 输出: 3第一维长度 Console.WriteLine(matrix.GetLength(1)); // 输出: 4第二维长度 Console.WriteLine(matrix.GetLowerBound(0)); // 输出: 0第一维下界 Console.WriteLine(matrix.GetUpperBound(0)); // 输出: 2第一维上界 Console.WriteLine(matrix.GetLongLength(0)); // 输出: 3返回 long10. 其他实用方法Array.AsReadOnly()— 返回只读包装器int[] arr { 1, 2, 3, 4, 5 }; ​ IListint readOnly Array.AsReadOnly(arr); ​ Console.WriteLine(readOnly[0]); // 输出: 1 // readOnly[0] 99; // ❌ 运行时抛出 NotSupportedExceptionArray.Empty()— 获取空数组推荐代替new T[0]int[] empty Array.Emptyint(); Console.WriteLine(empty.Length); // 输出: 0 ​ // 多次调用返回同一实例避免不必要的内存分配 int[] empty2 Array.Emptyint(); Console.WriteLine(ReferenceEquals(empty, empty2)); // 输出: TrueArray.ConstrainedCopy()— 原子性复制要么全成功要么全失败int[] source { 10, 20, 30 }; int[] dest new int[5]; ​ Array.ConstrainedCopy(source, 0, dest, 1, 3); Console.WriteLine(string.Join(, , dest)); // 输出: 0, 10, 20, 30, 0 方法速查表分类方法作用排序Sort升序排序反转Reverse反转元素顺序查找IndexOf/LastIndexOf按值查找索引搜索BinarySearch二分查找需先排序条件查找Find/FindAll/FindIndex/FindLast/FindLastIndex按条件查找条件判断Exists/TrueForAll判断是否满足条件遍历ForEach对每个元素执行操作清空Clear重置为默认值复制Copy/CopyTo/Clone/ConstrainedCopy复制数组调整大小Resize改变数组大小创建CreateInstance/Empty动态创建数组只读AsReadOnly返回只读包装器维度GetLength/GetLowerBound/GetUpperBound获取维度信息读写GetValue/SetValue按索引读写元素实践建议对于简单的查找、过滤、排序等操作现代 C# 更推荐使用 LINQ如arr.Where()、arr.OrderBy()代码更简洁。Array的静态方法在性能敏感场景下更有优势因为 LINQ 会产生额外的迭代器开销。
返回列表