C#通过Array.Clear部分清除数组,这个方法不但可以清除整个数组,还可以部分清除
using System; class ArrayClear { public static void Main() { int[] integers = { 1, 2, 3, 4, 5 }; DumpArray ("Before: ", integers); Array.Clear (integers, 1, 3); DumpArray ("After: ", integers); } public static void DumpArray (string title, int[] a) { Console.Write (title); for (int i = 0; i < a.Length; i++ ) { Console.Write("[{0}]: {1, -5}", i, a[i]); } Console.WriteLine(); } }
这段代码的输出结果如下:
Before: [0]: 1 [1]: 2 [2]: 3 [3]: 4 [4]: 5 After: [0]: 1 [1]: 0 [2]: 0 [3]: 0 [4]: 5