聲明數組方法有三種:
//聲明數組方法1
int[] arr={1,2,3};
//聲明數組方法2
int[] arr=new int[3];
arr[0]=1;
arr[1]=2;
arr[2]=3;
//聲明數組方法3
int[] arr=new int [ArrLenth];
int[] arr={1,2,3};
//聲明數組方法2
int[] arr=new int[3];
arr[0]=1;
arr[1]=2;
arr[2]=3;
//聲明數組方法3
int[] arr=new int [ArrLenth];
方法3比較特別,下例協助理解:
static void Main()
{
int i=1;
Console.WriteLine("Please enter the array's length:");
i=Int32.Parse(Console.ReadLine());
PrintArr(i);
Console.ReadKey();
}
static void PrintArr(int ArrLenth)
{
//Án©ú¼Æ¤èªk3
int[] arr=new int [ArrLenth];
for (int i=0;i<arr.Length;i++)
arr[i]=i;
Console.WriteLine("Print Array's Value");
for(int i=0;i<arr.Length;i++)
Console.WriteLine("arr[{0}]={1}",i,arr[i]);
}
{
int i=1;
Console.WriteLine("Please enter the array's length:");
i=Int32.Parse(Console.ReadLine());
PrintArr(i);
Console.ReadKey();
}
static void PrintArr(int ArrLenth)
{
//Án©ú¼Æ¤èªk3
int[] arr=new int [ArrLenth];
for (int i=0;i<arr.Length;i++)
arr[i]=i;
Console.WriteLine("Print Array's Value");
for(int i=0;i<arr.Length;i++)
Console.WriteLine("arr[{0}]={1}",i,arr[i]);
}