一维数组:
namespace 数组
{
internal class Program
{
static void Main(string[] args)
{
#region 基本概念
//相同元素的集合,有一维、多维、交错数组等
#endregion
#region 数组的声明
int[] ints1;
int[] ints2 = new int[4];//声明有四个位置的数组,默认为0
int[] ints3 = new int[4] { 1, 2, 3, 4 };
int[] ints4 = new int[] { 1, 2, 3, 4 };
int[] ints5 = { };
#endregion
#region 数组的使用
int[] ints6 = { 1, 2, 3, 4, 5, 6 };
//数组长度
Console.WriteLine(ints6.Length);
//获取元素
Console.WriteLine(ints6[0]);
#endregion
}
}
}
多维数组和交错数组:
#region 二维数组
//声明
int[,] arr1;
int[,] arr2 = new int[1, 2];
int[,] arr3 = new int[2, 2] { { 1, 2 },
{ 1, 2 } };
int[,] arr4 = new int[,] { { 1, 2 },
{ 1, 2 } };
int[,] arr5 ={ { 1, 2 },
{ 1, 2 } };
//多维数组长度
Console.WriteLine(arr5.GetLength(0));//得到行
Console.WriteLine(arr5.GetLength(1));//得到列
//获取元素
Console.WriteLine(arr5[0, 1]);
#endregion
#region 交错数组
//数组的数组,每个维度不同
//交错的声明
int[][] a1;
int[][] a2 = new int[3][];
int[][] a3 = new int[3][] { new int[]{1, 2, 3, 4},
new int[]{1, 2, 3},
new int[]{1, 2},};
//交错数组长度获取
Console.WriteLine(a3.GetLength(0));//获取行数
Console.WriteLine(a3[0].Length);//获取第1行的列数
//遍历交错数组
for(int i = 0; i < a3.GetLength(0); i++)
{
for(int j = 0; j < a3[i].Length; j++)
{
Console.Write(a3[i][j] + " ");
}
Console.WriteLine();
}
#endregion
2057

被折叠的 条评论
为什么被折叠?



