数组,一维数组,二维数组,交错数组
一维数组:
static void Main(string[] args)
{
Array MyArr = Array.CreateInstance(typeof(int),5); //数组的基类
MyArr.SetValue(1,0);
MyArr.SetValue(2,1);
foreach (int i in MyArr)
{
Console.WriteLine(i);
}
//定义简单数组有三种方式
int[] intarr = new int[3] { 1, 2, 3 };
int[] intarr2 = new int[] { 1,2,3};
int[] intarr3 = { 1,2,3};
}
二维数组:
foreach遍历 与 for循环
int[,] arrint = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
arrint[0, 0] = 10;
foreach (int arr in arrint)
{
Console.WriteLine(arr);
}
//存储有明显的行和列的区别
for (int i = 0; i < arrint.GetLength(0); i++)
{
for (int j = 0; j < arrint.GetLength(1); j++)
{
Console.WriteLine(arrint[i, j]);
}
}
//运行结构如下
交错数组: 数组中的数组
foreach遍历 与 for循环
int[][] arrint = new int[3][];
arrint[0] = new int[] { 1, 2, 3 };
arrint[1] = new int[] { 4, 5 };
arrint[2] = new int[] { 6 };
arrint[0][0] = 10;
foreach (int[] i in arrint)
{
foreach (int j in i)
{
Console.WriteLine(j);
}
}
for (int r = 0; r < arrint.GetLength(0); r++)
{
for (int c = 0; c < arrint[r].Length; c++)
{
Console.WriteLine(arrint[r][c]);
}
}
//运行结构如下