C#中的数组类
Array是C#.Net中的类,在System命名空间中声明/定义。数组类具有许多内置的方法和属性,这使得数组操作(与数组相关的操作)非常容易。
其中一些是常用的:
Sort(array);
Reverse(array);
Copy(Source,dest,n);
GetLength(index)
Length
考虑使用所有这些方法/属性的程序:
using System;
class SDARRAY2
{
static void Main()
{
int [] arr = {80,20,30,12,89,34,1,3,0,8};
for(int i=0;i<arr.Length;i++)
Console.Write(arr[i]+" ");
Console.WriteLine();
Array.Sort(arr);
foreach(int i in arr)
Console.Write(i+" ");
Console.WriteLine();
Array.Reverse(arr);
foreach(int i in arr)
Console.Write(i+" ");
Console.WriteLine();
int []brr = new int [10];
Array.Copy(arr,brr,5);
foreach(int i in brr)
Console.Write(i+" ");
}
}输出结果
80 20 30 12 89 34 1 3 0 8 0 1 3 8 12 20 30 34 80 89 89 80 34 30 20 12 8 3 1 0 89 80 34 30 20 0 0 0 0 0 Press any key to continue . . .
上面的示例演示了Array类方法的使用。Sort()方法执行排序,Reverse()方法将数组的元素反向,Copy方法用于将一个数组复制到另一个数组。