/*
数组的定义及遍历方式
*/
namespace Frank
{
public class Test
{
public static void Main(string[] args)
{
#region 一维数组
int[] array0;//声明对象
array0 = new int[4];//定义
array0[0] = 1;//赋值
int[] array1 = new int[4];//声明并定义
array1[0] = 2;//赋值
int[] array2 = new int[1]{1};//声明定义初始化值
int[] array3 = new int[]{1};//声明定义初始化
int[] array4 = {1};//数组简写模式
Test2[] t2 = new Test2[]
{
new Test2{Age = 20},
new Test2{Age = 30}
};
//遍历一维数组
for(int p=0;p<t2.Length;p++)
{
System.Console.WriteLine(t2[p].Age);
}
#endregion
#region 多维数组
int[,] i = new int[2,1];//二维
i[0,0] = 1;
i[1,0] = 2;
//遍历二维数组
for(int j = 0; j < i.GetLength(0) ;j++)
{
for(int k = 0;k < i.GetLength(1); k++)
{
System.Console.Write(i[j,k]+" ");
}
}
System.Console.WriteLine("");
int[,,] i2 = new int[2,1,1];//三维
i2[0,0,0] = 0;
i2[1,0,0] = 1;
int[,,] i3 = {{{1,2},{3,4}},{{5,6},{7,8}}};//简写模式三维数组
//遍历三维数组
for(int t = 0; t < i3.GetLength(0); t++)
{
for(int y = 0; y < i3.GetLength(1);y++)
{
for(int u =0; u<i3.GetLength(2); u++)
{
System.Console.Write(i3[t,y,u]+" ");
}
}
}
System.Console.WriteLine("");
#endregion
#region 锯齿数组
int[][] i4 = new int[3][];
i4[0] = new int[]{1,2,3,4};
i4[1] = new int[]{1,2,3};
i4[2] = new int[]{1};
//遍历锯齿数组
for(int p = 0; p < i4.Length; p++)
{
for(int u =0; u< i4[p].Length; u++)
{
System.Console.WriteLine(i4[p][u]);
}
}
#endregion
}
}
public class Test2
{
public int Age{get;set;}
}
}